cargo clean on Mac, and what it misses.
In Rust, the big folder is almost never the shared cache. It's the target folder in every project you've built. Here's how to clear each, and what Cargo now clears on its own.
The short version
Run cargo clean in a project to delete its target folder, which is usually the biggest thing Rust leaves on your disk. For the shared cache of downloaded crates, move ~/.cargo/registry/cache and ~/.cargo/registry/src to the Trash; Cargo downloads what it needs again. Leave ~/.cargo/bin alone: that's where cargo and your installed tools live.
Where Cargo keeps its cache on a Mac
Cargo's home is ~/.cargo, or wherever CARGO_HOME points. Build output doesn't go there: it goes in a target folder inside each project.
| What | Default location | On one Mac |
|---|---|---|
| Each project's build output | your-project/target | Per project |
| Downloaded crates | ~/.cargo/registry/cache | Not installed on the Mac we measured |
| Unpacked crate source | ~/.cargo/registry/src | Not installed on the Mac we measured |
| Git dependencies | ~/.cargo/git | Not installed on the Mac we measured |
Rust isn't installed on the Mac this page was written on, so there are no measured sizes here. Check yours with:
du -sh ~/.cargo/registry ~/.cargo/git 2>/dev/null
find ~ -name target -type d -prune -exec du -sh {} + 2>/dev/null | sort -h | tail -20
Not every folder named target is Rust's: check for a Cargo.toml beside it before you delete one.
What cargo clean deletes
cargo clean # the whole target folder
cargo clean --release # only target/release
cargo clean --doc # only target/doc
cargo clean -p some-crate # one package's artifacts
cargo clean removes compiled dependencies, incremental compilation data, and your binaries for that one project. It's safe: cargo build makes it all again, which can take minutes for a project with many dependencies. It doesn't touch ~/.cargo.
Why target gets so big: it keeps separate builds for debug and release, for every target triple you build, and incremental data for every crate. Changing compiler versions or features adds more rather than replacing. A medium project's target folder is often several gigabytes, and tens of gigabytes isn't unusual.
How to clear Cargo cache in ~/.cargo
registry/cache: safe. The compressed.cratefile for every crate version you've downloaded.registry/src: safe. Those crates unpacked. Cargo unpacks them again from the cache, or downloads them.git: safe. Clones of dependencies that point at a git repository. Cargo clones them again.bin: keep.cargo,rustcshims from rustup, and everything you installed withcargo install.
Cargo has no stable command to clear its home folder, so moving those folders to the Trash is the usual way. Do it with no build running. Toolchains themselves live in ~/.rustup/toolchains; remove old ones with rustup toolchain uninstall.
How big the Cargo cache gets
Less than it used to. Since Rust 1.88, Cargo cleans its own cache automatically: it removes downloaded files it hasn't used in three months, and unpacked files it hasn't used in one month. So ~/.cargo/registry usually holds a gigabyte or two. The target folders are another story: nothing cleans those, so ten old projects can mean tens of gigabytes. cargo-sweep, a third-party tool, can remove only the stale parts of a target folder if you'd rather not rebuild from scratch.
Also writing Go? See go clean -cache. Or Gradle, and every tool's cache.
Disk Forecast finds this for you
Disk Forecast finds target folders only where a Cargo.toml sits beside them, so a folder that just happens to be called target is never listed. They appear as Rust build folders under Cleanup › Safe to clear › Build folders in old projects after 30 days without changes, or under Build folders in active projects before that. ~/.cargo/registry is under Package manager caches. Everything goes to the Trash. It's free.
Questions
What does cargo clean do?
It deletes the target folder of the current project: compiled dependencies, incremental data, and binaries. cargo build recreates it. It doesn't touch ~/.cargo or other projects.
How do I clear the Cargo cache?
Move ~/.cargo/registry/cache, ~/.cargo/registry/src, and ~/.cargo/git to the Trash with no build running. Cargo downloads crates again as needed. Keep ~/.cargo/bin, which holds cargo and installed tools.
Is it safe to delete the Rust target folder?
Yes. It only holds build output, and cargo build makes it again. The next build of that project takes as long as a first build.
Does Cargo clean its cache automatically?
Since Rust 1.88, yes: Cargo removes downloaded files unused for three months and unpacked files unused for one month from its home folder. It never cleans target folders.
Related guides
Know before it's full.
Free. No account, no subscription.
macOS 14 or later · Apple Silicon and Intel · brew install --cask coreyhaines31/tap/diskforecast