Is it safe to delete a Rust project’s target folder?
- Yes.
targetis everything Cargo compiled for the project, and none of it is source.cargo cleandeletes it the official way. - What it costs you: the next build compiles the project and every dependency from scratch, a full build rather than a quick incremental one.
- Does it come back? Yes, with the next
cargo build,cargo testor editor check. - SiftMac’s verdict: Rebuilds
1. What’s in target
Cargo writes all build output for a workspace into one target folder at the workspace root: compiled dependencies, your binaries and libraries, test executables, documentation, and incremental data that makes small rebuilds quick.
target/debug: the defaultdevprofile, used bycargo build,cargo runandcargo test.target/release: builds made with--release.target/<triple>/…: builds for other targets, made with--target, kept apart from the rest.target/doc: output ofcargo doc.
Each profile and each target gets its own build of every dependency, which is why target grows well past the size of the code.
2. Where it is
~/Projects/my-crate/target
Next to the workspace’s top Cargo.toml, unless CARGO_TARGET_DIR or the build.target-dir setting points somewhere else. To list the ones under your code folder with their sizes:
find ~/Projects -name Cargo.toml -execdir sh -c 'test -d target && du -sh "$PWD/target"' \; 2>/dev/null
3. How big it gets
It depends on the dependency tree, which profiles and targets you’ve built, and how long since anything cleared it. Cargo’s automatic cleanup covers its download cache in ~/.cargo, not target, so a project you keep building only gains. To measure, and to preview a clean:
du -sh target target/*
cargo clean --dry-run
cargo clean --dry-run prints a summary of what would be deleted without deleting it; add --verbose to list the files.
4. How to delete it
From the project folder, with no build running:
cargo clean
With no options, that deletes the whole target directory. Narrower options:
cargo clean --releaseremoves only thereleasebuilds.cargo clean --docremoves only the generated docs.cargo clean -p <crate>cleans only that package.
Deleting the folder in Finder does the same. First copy out any program you use straight from target/release, or install it with cargo install --path . so it lives in ~/.cargo/bin instead.
5. What happens afterwards
- The next build compiles every dependency again. It needs no downloads if Cargo’s registry cache still has the crates.
- Your editor’s rust-analyzer may start a check straight away and fill part of
targetagain. - Tests, benchmarks and docs rebuild the next time you run them;
Cargo.lockand your sources aren’t touched.
6. Keep it while you’re working
In a project you build every day, target is what keeps each build quick, so clearing it buys space at the price of one full build. Finished projects are the ones worth clearing. The crates themselves are stored once for all projects in Cargo’s registry.
Questions
Does cargo clean delete Cargo.lock or my code?
No. It only removes the target directory, or the parts of it you choose. Cargo.lock, Cargo.toml and src stay.
Why is my target folder so much bigger than my code?
It holds compiled copies of every dependency for each profile and target you’ve built, plus incremental data kept to speed up rebuilds.
Can several projects share one target folder?
Yes, by pointing CARGO_TARGET_DIR or build.target-dir at one folder. Then there’s a single place to clear, but cleaning it costs every project that uses it a full build.
How SiftMac handles it. Its Developer files category lists target folders that sit next to a Cargo.toml, in projects whose own files haven’t changed for 1, 3 or 6 months, as Rust build output. None is ticked until you tick it, and cleared ones are deleted rather than shelved, since the next build makes them again.
Download SiftMac · scanning is free, and so is your first 2 GB of cleaning · See how it works