Rust developers know the pain: a single project's target/ directory can easily reach 5-20GB. With multiple projects, the Cargo registry cache, and downloaded crate sources, it's common for Rust to consume 10-100GB+ of disk space on your Mac.
This happens because Rust compiles everything from source (including dependencies), keeps both debug and release artifacts, caches the entire dependency tree, and stores downloaded crate sources in ~/.cargo/registry/.
Before cleaning anything, let's see the full picture:
# Find all Rust target/ directories
find ~/Projects ~/Developer ~/Code ~/Documents -maxdepth 4 \
-type d -name "target" -exec test -f "{}/../Cargo.toml" \; \
-exec du -sh {} \; 2>/dev/null | sort -rh
# Check Cargo home directory
du -sh ~/.cargo/
# Detailed breakdown of Cargo home
du -sh ~/.cargo/registry/
du -sh ~/.cargo/registry/src/
du -sh ~/.cargo/registry/cache/
du -sh ~/.cargo/git/
du -sh ~/.cargo/bin/
Typical disk usage from Rust:
| Location | Typical Size | What It Contains |
|---|---|---|
| target/ (per project) | 5-20 GB | Compiled artifacts, deps, build cache |
| ~/.cargo/registry/src/ | 1-5 GB | Extracted crate source code |
| ~/.cargo/registry/cache/ | 500 MB - 3 GB | Downloaded .crate files |
| ~/.cargo/git/ | 200 MB - 2 GB | Git dependency checkouts |
| ~/.cargo/bin/ | 50-500 MB | Installed binaries |
The target/ directory is the single biggest space consumer. It contains compiled dependencies, intermediate build artifacts, and both debug/release binaries.
# Inside your project directory cargo clean # Check how much space was freed du -sh target/ 2>/dev/null || echo "target/ removed"
# Remove only debug artifacts (keep release builds) cargo clean --release # this is counter-intuitive -- it cleans RELEASE rm -rf target/debug/ # to clean debug only # Or be selective rm -rf target/debug/deps/ rm -rf target/debug/build/ rm -rf target/debug/incremental/
target/
debug/ # Debug build artifacts
deps/ # Compiled dependencies
build/ # Build script outputs
incremental/ # Incremental compilation cache
.fingerprint/ # Dependency tracking
[your-binary] # Your compiled binary
release/ # Release build artifacts (same structure)
doc/ # Generated documentation
package/ # cargo package output
cargo clean only removes built artifacts. Source code, Cargo.toml, and Cargo.lock are untouched. Your next cargo build will simply recompile everything (which may take a few minutes for large projects).
Every crate you've ever used is cached in ~/.cargo/registry/. This includes both compressed .crate files and extracted source code.
# Check total registry size du -sh ~/.cargo/registry/ # Compressed crate downloads du -sh ~/.cargo/registry/cache/ # Extracted source (usually larger) du -sh ~/.cargo/registry/src/ # Git-based dependencies du -sh ~/.cargo/git/
# Remove extracted source (will be re-extracted on next build) rm -rf ~/.cargo/registry/src/ # Remove downloaded .crate files (will be re-downloaded) rm -rf ~/.cargo/registry/cache/ # Remove git checkouts rm -rf ~/.cargo/git/checkouts/ # Nuclear: remove everything (keeps installed binaries) rm -rf ~/.cargo/registry/ rm -rf ~/.cargo/git/
~/.cargo/bin/ (your installed tools like cargo-edit, cargo-watch) or ~/.cargo/config.toml (your Cargo configuration).
cargo-sweep is the best way to clean Rust build artifacts intelligently. It removes only the artifacts that haven't been used recently, keeping what you're actively developing.
# Install cargo-sweep cargo install cargo-sweep # Remove artifacts older than 30 days cargo sweep --time 30 # Remove artifacts older than 7 days (more aggressive) cargo sweep --time 7 # Preview what would be removed (dry run) cargo sweep --time 30 --dry-run # Clean ALL target directories recursively from current dir cargo sweep --recursive --time 30
cargo-sweep can also track which toolchain was used and clean artifacts from old toolchains:
# Start tracking the current toolchain cargo sweep --stamp # Later, remove artifacts not built by the current toolchain cargo sweep --installed
cargo-cache gives you a detailed breakdown of Cargo's cache directories and lets you clean selectively.
# Install cargo install cargo-cache # Show detailed cache breakdown cargo cache # Typical output: # Cargo cache '/Users/you/.cargo': # Total: 3.42 GB # 107 installed binaries: 458.22 MB # Registry: 2.18 GB # Registry index: 371.41 MB # 2847 crate archives: 692.58 MB # 2847 crate source checkouts:1.13 GB # Git db: 302.41 MB # 47 bare repos: 220.80 MB # 65 repo checkouts: 81.61 MB # Clean with different strategies cargo cache --autoclean # smart cleanup cargo cache --remove-dir=all # remove everything cargo cache --gc # garbage collect
If you're rebuilding the same dependencies across multiple projects, sccache can share compiled artifacts between projects, reducing total disk usage and build times.
# Install sccache brew install sccache # or cargo install sccache # Configure Cargo to use sccache # Add to ~/.cargo/config.toml: # [build] # rustc-wrapper = "sccache" # Or set environment variable export RUSTC_WRAPPER=sccache # Check sccache stats sccache --show-stats # Clean sccache cache sccache --stop-server rm -rf ~/Library/Caches/Mozilla.sccache/
The most impactful cleanup: find and clean all target/ directories across your machine.
# Find all Rust projects and their target/ sizes
find ~/Projects ~/Developer ~/Code -maxdepth 4 \
-name "Cargo.toml" -exec dirname {} \; 2>/dev/null | while read dir; do
if [[ -d "$dir/target" ]]; then
size=$(du -sh "$dir/target" | cut -f1)
echo "$size $dir"
fi
done | sort -rh
# Clean ALL target/ directories (aggressive!)
find ~/Projects ~/Developer ~/Code -maxdepth 4 \
-name "Cargo.toml" -exec dirname {} \; 2>/dev/null | while read dir; do
if [[ -d "$dir/target" ]]; then
echo "Cleaning $dir/target..."
rm -rf "$dir/target"
fi
done
# Or use cargo-sweep recursively (smarter -- keeps recent artifacts)
cd ~/Projects && cargo sweep --recursive --time 14
# Add to ~/.zshrc
alias rustclean='echo "Cleaning Cargo registry..." && \
rm -rf ~/.cargo/registry/src/ && \
rm -rf ~/.cargo/registry/cache/ && \
echo "Sweeping old build artifacts..." && \
cd ~/Projects && cargo sweep --recursive --time 14 2>/dev/null; \
echo "Rust cleanup complete!"'
# crontab -e
# Clean Rust artifacts older than 14 days every Sunday at 4am
0 4 * * 0 find ~/Projects -maxdepth 4 -name "Cargo.toml" -exec dirname {} \; | xargs -I{} sh -c 'cd {} && cargo sweep --time 14' 2>/dev/null
0 4 * * 0 rm -rf ~/.cargo/registry/src/ 2>/dev/null
If you prefer a visual approach, ClearDisk is a free, open-source macOS menu bar app that detects Cargo target/ directories alongside 14+ other developer caches (Xcode, node_modules, Docker, Homebrew, pip) and shows you how much space each uses.
# Install with Homebrew brew tap bysiber/cleardisk && brew install --cask cleardisk
Instead of each project having its own target/, share one directory for all projects:
# Add to ~/.cargo/config.toml [build] target-dir = "/Users/you/.cargo/target" # Or set per-session export CARGO_TARGET_DIR="$HOME/.cargo/shared-target"
# In Cargo.toml -- reduce debug info size [profile.dev] debug = 1 # reduced debug info (instead of default 2) incremental = true [profile.dev.package."*"] opt-level = 2 # optimize deps even in debug mode
[profile.release] lto = "thin" strip = true # strip debug symbols codegen-units = 1
| Task | Command |
|---|---|
| Clean current project | cargo clean |
| Check target/ size | du -sh target/ |
| Check Cargo cache | du -sh ~/.cargo/registry/ |
| Clean registry source | rm -rf ~/.cargo/registry/src/ |
| Smart sweep (30 days) | cargo sweep --time 30 |
| Recursive sweep | cargo sweep --recursive --time 14 |
| Detailed cache stats | cargo cache |
| Auto-clean cache | cargo cache --autoclean |
| Find all target/ dirs | find ~ -name "Cargo.toml" -exec dirname {} \; |
| GUI cleanup (all caches) | brew tap bysiber/cleardisk && brew install --cask cleardisk |