Cargo Target Directories: Why Rust Projects Eat 10GB Each

Each Rust project's target/ directory can hit 5-15GB. With multiple projects, that's 50-150GB of build artifacts you can reclaim.

Each Rust project’s target/ directory can easily hit 5-15GB. If you have 10 projects on your machine, that’s 50-150GB of disk space consumed by build artifacts alone. On a 256GB or 512GB MacBook, that’s a significant chunk of your drive gone to compiled code you probably aren’t actively using.

This isn’t a bug. It’s a direct consequence of how Rust’s compilation model works — and once you understand the mechanics, you can make informed decisions about what to clean and how to prevent the bloat from coming back.

Why Rust Builds Are So Large

Rust’s compilation strategy optimizes for correctness and runtime performance at the cost of disk space. Several factors compound to produce those multi-gigabyte target/ directories.

Debug Info

By default, cargo build produces a debug build. The compiler embeds full DWARF debug information into every binary and library artifact — function names, variable locations, type definitions, line number mappings. This debug info alone can double or triple the size of a compiled artifact compared to what the actual machine code requires.

A simple “hello world” debug binary is around 3-4MB. A real application with a handful of dependencies? Easily 50-200MB for the final binary, plus all the intermediate artifacts.

Incremental Compilation

Rust’s incremental compilation keeps fine-grained intermediate results from previous builds so that recompilation only touches changed code. This is essential for fast iteration — without it, even a one-line change in a large project could mean a full rebuild taking several minutes.

The trade-off: these incremental artifacts live in target/debug/incremental/ and can grow to several gigabytes per project. Each code change produces new incremental snapshots, and old ones aren’t garbage collected aggressively.

Dependency Compilation

When you cargo build, the compiler doesn’t just compile your code — it compiles every one of your dependencies from source. A typical Rust project with 100-300 transitive dependencies (check yours with cargo tree | wc -l) will compile each of those crates and store the resulting .rlib files, metadata, and build script outputs in target/.

Unlike languages with precompiled package distributions (Python wheels, npm prebuilds), Rust compiles everything locally. This means your target/ directory contains compiled output for every crate in your dependency tree.

Multiple Build Profiles and Targets

The target/ directory grows further if you use multiple build configurations:

  • target/debug/ — your default cargo build output
  • target/release/cargo build --release output (separate compilation, separate artifacts)
  • target/<triple>/ — cross-compilation targets like aarch64-apple-darwin or wasm32-unknown-unknown

Each profile and target combination gets its own full set of compiled artifacts. If you cross-compile for two targets in both debug and release mode, you have four complete copies of your dependency tree.

Where Exactly the Space Goes

Let’s break down every location where Cargo stores data on your machine.

Per-Project: target/debug/

This is the primary build output for cargo build. It contains:

  • deps/ — compiled .rlib and .rmeta files for every dependency
  • build/ — output from build scripts (build.rs), including generated code and compiled C dependencies
  • incremental/ — incremental compilation cache (often the largest subdirectory)
  • .fingerprint/ — hashes Cargo uses to determine if a crate needs recompilation
  • The final binary or library artifact itself

Typical size: 2-10GB per project, depending on dependency count.

Safe to delete? Yes. Running cargo build regenerates everything. You lose cached compilation, so your next build will be a full rebuild.

Per-Project: target/release/

Same structure as debug/, but for cargo build --release. Release builds have different trade-offs: no debug info by default, full optimization passes, but the intermediate artifacts still take significant space.

Typical size: 1-5GB per project. Often smaller than debug due to lack of debug info, but optimized artifacts can be larger.

Safe to delete? Yes, same as debug.

Global: ~/.cargo/registry/

This is Cargo’s global package registry cache. It has two main subdirectories:

  • registry/cache/ — compressed .crate files downloaded from crates.io. These are the raw downloaded packages.
  • registry/src/ — extracted source code from those .crate files. Cargo extracts sources here before compiling them into your project’s target/ directory.

The registry is shared across all your projects — if ten projects depend on serde, Cargo downloads and extracts it once.

Typical size: 1-5GB, depending on how many unique crates you’ve used across all projects.

Safe to delete? Yes. Cargo re-downloads crates as needed. Deleting registry/cache/ forces re-download; deleting registry/src/ forces re-extraction.

Global: ~/.cargo/git/

When your Cargo.toml specifies a git dependency (git = "https://github.com/..." instead of a version from crates.io), Cargo clones the repository here.

  • git/db/ — bare git repositories (the full clone)
  • git/checkouts/ — checked-out working copies at specific commits

Typical size: 500MB-5GB, depending on how many git dependencies you use. Some large git dependencies (like forks of big frameworks) can add gigabytes on their own.

Safe to delete? Yes. Cargo re-clones as needed. You’ll pay the download cost again, but nothing is lost.

Cleanup Commands

Per-Project: cargo clean

The simplest approach. Run this from any Rust project directory:

cargo clean

This removes the entire target/ directory for that project. Your next cargo build will be a full rebuild from scratch.

To clean only release artifacts:

cargo clean --release

Registry Cache

Remove the global registry cache (all downloaded crate files):

rm -rf ~/.cargo/registry/cache
rm -rf ~/.cargo/registry/src

The registry/index directory contains the crates.io index — you can delete it too, but it’s smaller and takes time to re-download.

To clean git caches:

rm -rf ~/.cargo/git

Finding All Target Directories

The real problem isn’t one project — it’s the ten projects scattered across your filesystem that you haven’t touched in months. Find them all:

find ~ -name "target" -type d -maxdepth 5 | while read dir; do
  if [ -f "$dir/../Cargo.toml" ]; then
    size=$(du -sh "$dir" 2>/dev/null | cut -f1)
    echo "$size  $dir"
  fi
done

This finds target/ directories that are actually Rust projects (verified by the presence of Cargo.toml in the parent directory) and shows their sizes. Review the output, then clean the ones you want:

# Clean a specific project without cd-ing into it
cargo clean --manifest-path /path/to/project/Cargo.toml

cargo-sweep: Time-Based Cleanup

cargo-sweep is a community tool that removes build artifacts older than a specified number of days, keeping your recent work intact:

cargo install cargo-sweep

# Remove artifacts older than 30 days (run from project root)
cargo sweep --time 30

# Or stamp current artifacts, then later sweep unstamped ones
cargo sweep --stamp
# ... work on the project ...
cargo sweep --file

This is more surgical than cargo clean — it preserves incremental compilation cache for code you’re actively working on while removing artifacts for features or dependencies you changed weeks ago.

To sweep across all projects:

find ~ -name "Cargo.toml" -maxdepth 5 | while read manifest; do
  project_dir=$(dirname "$manifest")
  if [ -d "$project_dir/target" ]; then
    echo "Sweeping $project_dir"
    cargo sweep --time 30 --manifest-path "$manifest"
  fi
done

The Shared Target Directory Trick

If you work on multiple Rust projects, you can point them all at a single shared target/ directory using the CARGO_TARGET_DIR environment variable:

# Add to your ~/.zshrc or ~/.bashrc
export CARGO_TARGET_DIR="$HOME/.cargo-target"

Or set it in ~/.cargo/config.toml:

[build]
target-dir = "/Users/yourname/.cargo-target"

What this does: All projects compile into the same directory. Shared dependencies are compiled once and reused across projects — if five projects depend on tokio, it’s compiled once instead of five times.

What this saves: Potentially 50-70% of total target directory space if your projects share many dependencies.

The caveat: You can no longer cargo clean a single project without wiping artifacts for all projects. This trade-off works best if you have many small projects with overlapping dependency trees. You can also set this per-project in .cargo/config.toml at the project root for monorepo setups.

The Automated Way

Manual cleanup works, but it’s tedious. Most developers do this once every few months when they get a “disk full” warning — by which point 50-100GB of stale build artifacts have accumulated.

MegaCleaner automates this with a dedicated Rust scanner. It searches your configured project directories for target/ folders alongside Cargo.toml files, and separately finds your global Cargo caches at ~/.cargo/registry/ and ~/.cargo/git/. Each location is sized and presented individually so you can review before cleaning.

The scanner has two sub-features you can toggle independently:

  • Cargo — finds and sizes your ~/.cargo/registry and ~/.cargo/git caches
  • Build Output — discovers every target/ directory in your projects, reads the project name from Cargo.toml, and reports the size

It also checks staleness by examining the last-modified dates of source files (.rs, .toml, .json, .yaml, .yml) in each project. If you haven’t touched a project’s source code in a while, MegaCleaner flags that target/ directory as stale and pre-selects it for cleanup. Projects you’re actively working on are left unselected by default.

Rust is just one of MegaCleaner’s 29 scanners (21 developer-focused, 8 general). It handles Node.js node_modules, Xcode DerivedData, Python virtual environments, Docker images, and more — all the caches that quietly consume your disk across different tech stacks. One-time purchase, $49, native macOS app.

Quick Reference

LocationWhat It StoresTypical SizeCleanup Command
<project>/target/debug/Debug build artifacts, incremental cache2-10 GBcargo clean
<project>/target/release/Release build artifacts1-5 GBcargo clean --release
~/.cargo/registry/cache/Downloaded .crate files500 MB-2 GBrm -rf ~/.cargo/registry/cache
~/.cargo/registry/src/Extracted crate source code500 MB-3 GBrm -rf ~/.cargo/registry/src
~/.cargo/git/Git dependency clones500 MB-5 GBrm -rf ~/.cargo/git

Total across 10 projects: 30-150+ GB

Everything in this table is safe to delete. Cargo rebuilds and re-downloads automatically. The only cost is time — a full rebuild of a large project might take 5-10 minutes, and re-downloading dependencies requires an internet connection.

For ongoing maintenance, pick one approach:

  • Manual: Run cargo clean on projects you haven’t touched in a month
  • cargo-sweep: cargo sweep --time 30 for time-based cleanup
  • Shared target: CARGO_TARGET_DIR to deduplicate dependencies across projects
  • Automated: MegaCleaner to scan, size, and clean everything with staleness detection

Your Mac’s SSD is too valuable for build artifacts from six months ago. Reclaim it.