ClearDisk
Buy for $10Download ClearDiskDownload
A LITTLE MORE CLARITY

How to clear the Cargo cache on Mac

To clear the Cargo cache on Mac, delete ~/.cargo/registry and ~/.cargo/git while editors are closed, then run cargo clean inside inactive Rust projects to remove their target folders. Cargo re-downloads crates on the next build, while cargo clean reclaims the largest per-project build artifacts.

1. Measure the three places Rust uses

Rust uses space in three places: Cargo’s home, ~/.cargo unless CARGO_HOME is set; rustup’s home, ~/.rustup, which holds installed toolchains; and a target folder inside every project you have built. The first two are hidden in Finder because their names start with a dot.

The find command below lists target folders under a projects folder with their sizes. Other build tools, Maven among them, also name their output folder target, so check for a Cargo.toml next to any folder before treating it as Rust output.

du -sh ~/.cargo/registry ~/.cargo/git ~/.rustup
find ~/Projects -type d -name target -prune -exec du -sh {} + 2>/dev/null | sort -h

What is inside ~/.cargo

Cargo’s documentation describes the layout. registry/index holds metadata about available crates, registry/cache the downloaded .crate archives, and registry/src the extracted sources the compiler reads. git/db and git/checkouts do the same for dependencies fetched from git repositories. bin holds programs installed with cargo install, and the cargo and rustc commands themselves when rustup installed them.

The documentation says you can remove any part of the cache and Cargo will do its best to restore the sources, by re-extracting archives, checking out repositories or downloading again. Since Rust 1.88, Cargo also cleans its global cache automatically: files downloaded from the network are removed if not accessed for three months, and files obtained from the local system after one month. The cache.auto-clean-frequency setting controls this; the release notes suggest setting it to never if you also use older Cargo versions alongside current ones.

2. Clear the registry and git caches

Finish any builds and quit your editor, since rust-analyzer reads these folders in the background. Then move ~/.cargo/registry and ~/.cargo/git to the Trash, or only their cache and src subfolders for a smaller step. The next build downloads what each project needs, so work offline is not possible until it has. Before a trip, cargo fetch in a project downloads its dependencies without building anything.

Keep bin, config.toml and credentials.toml; the last holds tokens for publishing to registries. ClearDisk’s Developer Junk list includes the Rust crate cache at ~/.cargo/registry with its size, which helps you compare it with target folders before choosing.

3. Clean target folders with cargo clean

cargo clean, run inside a project, deletes its whole target folder, and --dry-run shows a summary of what would be deleted first. --release removes only release artifacts and --doc only generated documentation. In a workspace, members share one target folder at the workspace root, so run it there. If CARGO_TARGET_DIR is set, or a Cargo configuration sets a target directory, builds go to that shared folder instead of each project, and the find command above will not see them.

A cleaned project rebuilds from scratch on its next build, which for large dependency trees takes a while. Old experiments and finished projects are the best candidates; the project you build every day will simply fill its folder again.

cargo clean --dry-run
cargo clean
cargo clean --release

4. Remove toolchains you no longer use

Every toolchain rustup installs, such as stable, a pinned version or a dated nightly, is a full compiler with its standard library. rustup toolchain list shows them, and rustup toolchain uninstall removes one by name. Before removing a pinned version, check whether a project’s rust-toolchain.toml file still names it. Extra compilation targets, such as those for iOS or WebAssembly, are installed per toolchain as well: rustup target list --installed shows them and rustup target remove deletes one.

To remove Rust completely, rustup’s documentation gives rustup self uninstall, which removes rustup, its toolchains and Cargo’s home. Copy any program in ~/.cargo/bin you still want to keep somewhere else first.

rustup toolchain list
rustup toolchain uninstall nightly-2025-01-01

Common questions

Is there a cargo clear cache command?

No. Cargo has no built-in cache or clear-cache subcommand; on Cargo 1.98.1 both names return “no such command”. The built-in cargo clean removes a project’s target folder, not the downloaded crates in ~/.cargo/registry. To clear that cache, delete the registry and git folders yourself or let Cargo’s automatic cleaning remove old files.

Does Cargo clean up its own cache automatically?

Yes, since Rust 1.88. Files downloaded from the network are removed if unused for three months, and files obtained locally after one month, with the cache.auto-clean-frequency setting controlling how often this runs.

Is it safe to delete the ~/.cargo/registry folder?

Yes. Cargo can remove any part of the cache and will restore what it needs by re-extracting archives, checking out repositories, or downloading again. Just keep bin, config.toml and credentials.toml, which hold tools and publish tokens.

Why doesn't cargo clean free space from every project's build folder?

If CARGO_TARGET_DIR is set, or a project configures a shared target directory, builds go there instead of each project's own folder, so searching for individual target folders won't find that shared location.

Can I remove a Rust toolchain that rustup installed?

Yes, with rustup toolchain uninstall, but check first whether a project's rust-toolchain.toml file still names that pinned version, since removing a toolchain a project depends on breaks its build.

References & further reading

These guides combine the linked official guidance with ClearDisk’s documented behavior. Your files and app settings determine the right next step.

Main guide: developer storage on Mac · Related pillar: clear Xcode Derived Data on Mac · Browse all Developer storage guides →

FROM A NUMBER TO AN ANSWER

See what’s using
your space.

Explore your Mac with ClearDisk’s free local scanner.
Review the files. Keep the control.

Download free scannermacOS 15+ · version 2.2.1Cleanup license: $10 once