47

When I run cargo build, various libs get stored within the folder /usr/local/lib/rustlib/.

What is the correct way to clear these libs? I could rm these files manually, but would that be the right thing to do? I noticed that /usr/local/lib/rustlib/manifest is a file containing a list of the fill file paths of all the libs, and hence might be breaking something if I remove these files manually.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
bojangle
  • 888
  • 1
  • 8
  • 14

2 Answers2

37

Install and run cargo-cache to clear the cache in the ~/.cargo folder:

cargo install cargo-cache
cargo cache -a
Simson
  • 3,373
  • 2
  • 24
  • 38
  • 1
    error caused by "feature `resolver` is required" :sad_face: – Mathieu CAROFF Aug 22 '21 at 12:48
  • error: no such subcommand: `cache` – chovy Mar 03 '22 at 22:58
  • 5
    @chovy did you remember to install cargo-cache first? – Michael Mc Donnell Mar 04 '22 at 18:10
  • 1
    If you no longer have any need of any of the cargo crates, such as just wanting to compile a project and then freeing up the space afterward, the command to use is `cargo-cache --remove-dir all` (or `cargo-cache -r all` for short). – Vopel Dec 07 '22 at 20:20
  • I found this useful when using dependencies with versions that cargo does not support. For example, Cargo will not download the correct dependency when you switch a dependency from `1.2.0-dev.1` to `1.2.0-dev.2`. It doesn't respect semver "pre-release versions" (Cargo doesn't document this) semver "build metadata" (documented). – Ben Butterworth Jan 30 '23 at 23:14
  • I ran into an issue with the cache that made me unable to install/build `cargo-cache` normally. What worked for me was to just update rust (`rustup update`) so it wouldn't use cached builds made with previous versions of rustc. This was only possible since I did not need to use a specific version of rustc, but it did help give me the peace of mind that I was not going to break something by manually deleting files. Afterwards, I was able to install `cargo-cache` and have it do a full cache reset. – Locke Aug 06 '23 at 21:20
28

I believe that manifest file is just for the built-in libraries, i.e. those distributed with rustc. cargo itself stores things in ~/.cargo (for the moment), if you do wish to just remove all the libraries then deleting that directory won't break anything.

If you're just wanting cargo to rebuild/update dependencies you can run cargo update.

timlyo
  • 2,086
  • 1
  • 23
  • 35
huon
  • 94,605
  • 21
  • 231
  • 225