How to clean Rust target/ folders

Every Rust project keeps debug, release and incremental build artifacts in target/, compiled per profile and per toolchain. Across a few old projects that is easily tens of GB. Cargo never cleans it for you.

These commands permanently delete files. Most are caches the tool rebuilds on its own (the next build or install is slower and may need internet). Anything that can hold data you cannot re-download is flagged on the page. Read each command, then copy and run it yourself. devdisk never runs cleanup commands for you.

Find the biggest target folders

macOS / Linux
du -sh ~/code/*/target 2>/dev/null | sort -hr | head
Windows (PowerShell)
Get-ChildItem $HOME\code -Directory | ForEach-Object { $t = Join-Path $_.FullName "target"; if (Test-Path $t) { "{0,8:N1} GB  {1}" -f ((Get-ChildItem $t -Recurse -File | Measure-Object Length -Sum).Sum / 1GB), $_.Name } }

Replace ~/code with wherever your projects live.

Clean one project

Any OS (inside the project)
cargo clean             # whole target/
cargo clean --release   # release artifacts only
cargo clean --doc       # generated docs only

Only build output goes. Your source and Cargo.lock stay. The next build is a full rebuild.

Trim instead of wiping

Any OS
cargo install cargo-sweep
cargo sweep --time 30    # artifacts not used in 30 days
cargo sweep --installed  # artifacts from toolchains you no longer have

cargo-sweep is a third-party tool. Run it inside a project, or pass -r to walk a folder of projects.

The ~/.cargo registry

Downloaded crates live in ~/.cargo/registry (%USERPROFILE%\.cargo\registry on Windows). Since Cargo 1.88, Cargo deletes downloaded crate files it has not used in 3 months on its own. This automatic cleanup does not cover target/.

Measure with devdisk

devdisk is a free, read-only scanner. It finds every target/ that sits next to a Cargo.toml and lists it under Safe to regenerate, and lists ~/.cargo/registry under Review first. It never deletes anything; it only shows sizes and writes a cleanup script you read and run yourself.

macOS / Linux
curl -fsSLO https://disk.sadana.app/devdisk.py && python3 devdisk.py
Windows (PowerShell)
iwr https://disk.sadana.app/devdisk.py -OutFile devdisk.py; py devdisk.py

Run free scannerSample report

Want one-click cleanup?

A planned devdisk desktop app for Mac and Windows: live treemap, cleanup that moves files to the Trash, scheduled scans. One email when it launches, with an early-supporter price. Your address is used only for that, and every email has an unsubscribe link.

FAQ

Does cargo clean delete my code?

No. It only removes the target directory (or part of it with --release / --doc).

Why is target/ so large?

Debug info, incremental compilation data, and a separate copy of every dependency per profile and toolchain.

Can I share one target folder across projects?

Yes, with CARGO_TARGET_DIR. It saves space on shared dependencies, but builds of different projects then wait on the same lock.

More cleanup guides