cargo icon indicating copy to clipboard operation
cargo copied to clipboard

cargo clean ~/.cargo

Open ralt opened this issue 10 years ago • 21 comments

View all comments

Would it be possible to have a cargo clean command for all the downloaded deps in ~/.cargo? I'm currently using a small HDD (Chromebook) and would like to cleanup whatever I can, included sources and the local index (not sure it's in ~/.cargo).

ralt avatar Nov 13 '16 13:11 ralt

Also, I'm not sure what I can actually safely delete.

ralt avatar Nov 13 '16 13:11 ralt

In general it's always safe to completely rm -rf ~/.cargo (unless you've got your own data there like config, installed crates, rustup, etc). Seems fine to have something like cargo clean --global though!

alexcrichton avatar Nov 14 '16 15:11 alexcrichton

FWIW, it looks like you can safely delete ~/.cargo/registry and the binaries under ~/.cargo/bin will keep working. At least they did when I moved the registry directory elsewhere.

dlukes avatar Sep 20 '17 13:09 dlukes

Hey Florian - just ran into this - 👍 would have expected this to be built in.

benjamingr avatar Jul 29 '18 13:07 benjamingr

I made a small plugin that prints cache size and can clean out different parts of it, feel free to try it out: https://github.com/matthiaskrgr/cargo-cache

matthiaskrgr avatar Jul 29 '18 13:07 matthiaskrgr

rm -rf ~/.cargo blows away your rustup binaries in PATH, I do not recommend.

alexchandel avatar Oct 18 '18 22:10 alexchandel

The following command in addition to what has been mention may help saving disk space. Go into the folder containing all your rust projects:

find `pwd` -maxdepth 5 -type d -name 'target' | grep -v supercurrentproject | xargs rm -rf

It wont do anything a cargo build can fix again.

chevdor avatar Oct 19 '18 16:10 chevdor

This is a sorta old bug, it seems real weird that this is still not a supported feature of cargo. Most package managers have a "clear cache files" command. I feel like I shouldn't have had to search google to find a github issue with scary "manually delete these files" instructions, I should have been able to find how to do it just by typing "cargo help"…

mcclure avatar Apr 16 '19 18:04 mcclure

For anyone interested I have released cargo-cache on crates.io a few months ago, cargo install cargo-cache to install.

rm -rf ~/.cargo blows away your rustup binaries in PATH, I do not recommend.

cargo-cache can remove dirs selectively via --remove-dir git-repos,registry-sources (this for example will only remove checkouts of cloned git repos but not touch the original repos and also remove unpacked crate sources).

To be extra sure you can perform a --dry-run first which will tell you the directory paths that would be removed.

If you have any additional ideas or feature requests, I would be glad to hear them. :)

matthiaskrgr avatar Apr 20 '19 13:04 matthiaskrgr

Deleting ~/.cargo/registry seems like a decent workaround.

The downside is that it deletes both crates sources and index, so the whole index has to be downloaded all over again after each cleanup, and it's not small.

An option to cleanup source but keep the index would be nice.

guillaume-uH57J9 avatar Apr 03 '22 11:04 guillaume-uH57J9

You can delete ~/.cargo/registry/{cache,src} to only delete crate sources. (cache has downloaded .crate tarballs, src has unpacked sources)

bjorn3 avatar Apr 03 '22 11:04 bjorn3

I guess still not a thing...

johnalanwoods avatar May 16 '23 20:05 johnalanwoods

#10070 added a --with-downloads flag to cargo clean to clear all user-wide downloaded files. The name was felt to be more clear than --global.

My concern with this approach is that cargo clean is a focused on a workspace and this opens a can of worms for user expectations. Does this clean only whats relevant for the current project, for the current project thats not referenced by others, or everything?

We also need to keep in mind other kinds of global caches that we will have

  • rust-lang/rfcs#3424 (shared target dir for scripts)
  • #11156 (central location for all target dirs)
  • #5931 (per-user cache)

I think it would also help to have a design that also explores prior art

epage avatar May 17 '23 13:05 epage

NPM and Composer are package managers for languages that aren't compiled, so there also isn't any cache for compiled artifacts. I don't get what distinction between different types of caches you are trying to make.

bjorn3 avatar Sep 22 '23 18:09 bjorn3

FYI we are working on global cache garbage collection. Its being tracked in #12633

epage avatar Oct 11 '23 15:10 epage

I'm going to close in favor of #12633. I don't think this issue tracks anything beyond what is implemented in the tracking issue.

ehuss avatar Nov 28 '23 18:11 ehuss

I made a small plugin that prints cache size and can clean out different parts of it, feel free to try it out: matthiaskrgr/cargo-cache

Thank you so much for this tool. 5G of data space saved! :rocket:

kierun avatar Apr 17 '24 08:04 kierun

Thanks a lot!

bobvasic avatar Jun 30 '25 17:06 bobvasic

For any future visitor stumbling upon this, a decent middle ground solution is:

 rm -rf ~/.cargo/registry/src/*/*/ ~/.cargo/registry/cache/*/

This is far more aggressive than the garbage collection proposal in the works but it doesn't clear the index. It shrunk my .cargo from 2.4GB down to 2.2GB.

jaygiffin avatar Sep 27 '25 02:09 jaygiffin

A cargo clean ~/.cargo (or equivalent for the shared registry/cache) would be very useful. The target/ directory cleanup per-project is straightforward, but the shared caches in ~/.cargo/ are harder to manage.

Quick audit of what's consuming space:

# Total cargo home
du -sh ~/.cargo/

# Break it down
du -sh ~/.cargo/registry/cache/    # compressed crate archives
du -sh ~/.cargo/registry/src/      # extracted source code
du -sh ~/.cargo/git/               # git checkouts for git dependencies

# Find all target/ directories across projects
find ~/Projects -name "target" -type d -maxdepth 4 2>/dev/null | \
  while read d; do echo "$(du -sh "$d")"; done | sort -rh | head -10

Current cleanup approach:

# Clean specific project
cargo clean

# Clean old registry entries (cargo-cache tool)
cargo install cargo-cache
cargo cache --autoclean

# Manual registry cleanup
rm -rf ~/.cargo/registry/src/     # re-extracted on next build
rm -rf ~/.cargo/registry/cache/   # re-downloaded on next build

Having built-in cache GC with configurable retention would solve this properly. The cargo-cache crate helps but it's an external tool.

On macOS, Rust caches are typically one of many developer disk hogs:

  • target/ directories (5-30GB across projects)
  • ~/.cargo/ registry (1-10GB)
  • Docker images (10-60GB)
  • Xcode DerivedData (5-40GB)
  • Homebrew (2-15GB)
  • node_modules (5-30GB)

ClearDisk (free, open-source, macOS native) scans all of these at once and shows a complete breakdown -- including Cargo registry and target directories.

bysiber avatar Mar 02 '26 12:03 bysiber