cargo clean ~/.cargo
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).
Also, I'm not sure what I can actually safely delete.
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!
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.
Hey Florian - just ran into this - 👍 would have expected this to be built in.
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
rm -rf ~/.cargo blows away your rustup binaries in PATH, I do not recommend.
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.
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"…
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. :)
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.
You can delete ~/.cargo/registry/{cache,src} to only delete crate sources. (cache has downloaded .crate tarballs, src has unpacked sources)
I guess still not a thing...
#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
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.
FYI we are working on global cache garbage collection. Its being tracked in #12633
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.
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:
Thanks a lot!
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.
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.