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 ofcargo build,cargo testandcargo 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 fromcargo 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
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:
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.