Why is my Rust target folder so big? (And how to clean it)

What lives in Cargo's target directory, why it grows to gigabytes, and how to shrink it safely: cargo clean, cargo-sweep and a shared CARGO_TARGET_DIR.

Nov 1, 20262 min read

One bin per part of target/, as tall as its sizeNeedle: the total on diskAn example workspace's target/ on a scale: 1.4 GB of compiled dependencies, 0.9 GB of incremental cache, 0.3 GB of build-script output, 0.8 GB of release build and 0.1 GB of docs. Each cargo clean variant lifts its bins off; plain cargo clean empties the pan.

If you’ve written Rust for a while, you’ve seen it: a few thousand lines of code and a target folder measured in gigabytes. It isn’t a bug. Cargo trades disk for build speed, aggressively. Here’s what it keeps, and how to take some back.

What’s inside target/

  • target/debug/ holds the output of cargo build, cargo test and cargo run. Every dependency is compiled here, with full debug info by default.
  • target/release/ holds optimized builds from --release, a second full set.
  • target/debug/incremental/ is the incremental-compilation cache. It makes rebuilds fast, and it’s frequently the biggest single directory.
  • target/debug/deps/ contains compiled dependencies, including old versions left behind when you update a crate or change features. Cargo doesn’t garbage-collect these.
  • Per-target triples like target/aarch64-apple-darwin/ appear when you cross-compile, as yet another full set.
  • target/doc/ comes from cargo doc.

Switching toolchains (stable, nightly, a new compiler release) also produces fresh artifacts while the old ones stay put.

Is it safe to delete?

Yes. target is entirely generated. Delete it and the next cargo build recompiles everything: correct, just slower the first time. Your Cargo.lock and source are untouched.

Cleaning options, from blunt to precise

cargo clean

Removes the whole target directory for this workspace.

Make target grow slower

Share one target directory

For lots of small experiments, point Cargo at one shared directory so common dependencies compile once:

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

The downside is that concurrent builds of different projects contend for a lock, and one cargo clean clears everything. Many people only do this for scratch projects.

Emit less debug info

Full debug info is a large share of target/debug. If you don’t step through dependencies in a debugger, reduce it in Cargo.toml:

[profile.dev]
debug = "line-tables-only"   # keeps file:line in backtraces

[profile.dev.package."*"]
debug = false                # no debug info for dependencies

Prune after big updates

After cargo update or a toolchain upgrade, old artifacts stick around. That’s the moment to run cargo clean (or cargo sweep) on projects you’re actively building.

How much could you get back?

Rust projects dominate disk usage quickly. Try a couple more in this estimate:

Estimated reclaimable
0.0 GB
0.0% of a 512 GB disk

Uses rough per-project averages (node_modules ≈ 350 MB, target ≈ 1.5 GB, build + .dart_tool ≈ 800 MB, .build ≈ 400 MB, .gradle / target ≈ 300 MB). Your numbers will differ; Gigaflush measures the real ones.

For the projects you’re not touching this month, the answer is simple: delete target and move on. You’ll pay one cold build when you come back. That’s a good trade for gigabytes now.

Keep reading