cargo clean: how to clear the Cargo cache on Mac

To clear the Cargo cache on Mac with cargo clean or by hand, it helps to know that Rust keeps two different things. Each project’s target folder holds everything Cargo built for it, often gigabytes per project, while the crates Cargo downloaded live apart, in a cache shared by all projects. This guide shows how to clean both, starting with old projects, and what Cargo removes by itself.

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned
  1. Open MacWashing and choose System Junk in the sidebar.
  2. Click Start Scan.
  3. Look through what it found and click Clean. Anything debatable stays unchecked.

Free trial · macOS 14 or later

What cargo clean deletes

Every Cargo project builds into its own target folder next to Cargo.toml: compiled dependencies, debug and release builds, documentation, incremental build data. Cargo makes all of it again from your code and the downloaded crates, so deleting it costs only build time.

This command shows what would be deleted and how much, without deleting anything. Run it in the project folder:

cargo clean --dry-run

How to run cargo clean

This command deletes the whole target folder of the project you are in. Your code, Cargo.toml, Cargo.lock and the Cargo cache stay. The next cargo build compiles everything again, which takes minutes on a big project:

cargo clean

To delete less, cargo clean --release removes only the release build, and cargo clean --doc only the documentation made by cargo doc. In a workspace, one run cleans all its members.

Delete target folders of old projects

A project keeps its target folder for years after you last built it. Projects you no longer work on are the easiest space to free: run cargo clean in each, or move its target folder to the Trash. The code stays, and the next build makes the folder again.

How to clear the Cargo cache

Cargo keeps the crates it downloaded in ~/.cargo, shared by all projects:

  • ~/.cargo/registry/index — information about crates: their versions and dependencies.
  • ~/.cargo/registry/cache — downloaded crates as .crate archives.
  • ~/.cargo/registry/src — the same crates unpacked for the compiler.
  • ~/.cargo/git/db and ~/.cargo/git/checkouts — dependencies taken from Git repositories.

Cargo has no stable command that clears all of it. The Cargo Book says any part of the cache can be removed: Cargo unpacks or downloads again what a build needs. To clear it by hand:

  1. Quit your code editor and wait for running builds to finish.
  2. In Finder, choose Go › Go to Folder (Shift-Command-G), type ~/.cargo/registry and press Return.
The Go to Folder panel with ~/.cargo/registry typed
  1. Move the cache and src folders to the Trash. To clear Git dependencies too, open ~/.cargo/git the same way and move checkouts and db to the Trash.
Finder, ~/.cargo/registry: the cache and src folders selected
  1. Empty the Trash.

Don’t delete ~/.cargo/bin: it holds cargo, rustc and the tools you installed with cargo install. Leave ~/.cargo/config.toml too: it is Cargo’s settings, not cache.

Does Cargo clear its cache by itself?

Partly, since Rust 1.88. During builds Cargo deletes files in ~/.cargo it hasn’t used for a while: downloaded ones after 3 months, ones it can make again offline after 1 month. Old target folders are never cleaned automatically: they stay until you run cargo clean or delete them.

cargo-cache, a third-party tool the Cargo Book mentions, shows the size of each part of the cache and clears the parts you pick. The built-in cargo clean gc exists only in nightly Rust so far.

With MacWashing, it’s one click

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned

System Junk in MacWashing lists the Cargo cache under App Caches. Project Dependencies shows the target folders of Rust projects you haven’t changed for 90 days, unchecked: building them again takes time, so you decide. The caches of your other apps are cleared in the same pass.

Free trial · macOS 14 or later

Questions and answers

Will MacWashing delete my own files when it cleans?

No. It removes only what it can match to a known app or a known kind of junk, in places apps leave files; macOS and Apple’s files are never touched. Anything debatable is marked and stays unchecked until you decide.

What about apps that are open?

If an open app holds files being cleaned, MacWashing waits for it to quit or quits it at your request, and opens it again afterwards.

Is it free?

Yes, within the free trial: System Junk and the Uninstaller free up to 25 GB in total, with no account or card needed.

Is it safe to run cargo clean?

Yes. It deletes only what Cargo built, the target folder. Your code, Cargo.lock and downloaded crates stay. The next build takes as long as the first one.

Will clearing the Cargo cache break my projects?

No. Cargo downloads or unpacks again the crates a build needs. Without the internet, a project whose crates were deleted won’t build until you are online again.

Why is the target folder so big?

Cargo keeps separate debug and release builds, for every target and toolchain you used, plus incremental build data. Every dependency is compiled into the project’s own target, so ten projects with the same crates hold ten copies.