Garbage collect whole `target/`
@0xPoe is already working on this.
Since Aug 30, 2026.
Assessment
This issue has not been assessed yet.
Description
Note: for cleaning individual build units within a build-dir, see #6229
Problem
With "cargo script", the target directory is "hidden" from the user, making it easy to leak when you delete your script.
If we move forward with rust-lang/rfcs#3371, a similar situation will happen for regular packages.
If I haven't touched a project in a long while but have run rustup update, there might be nothing of use left in target/, wasting space.
Sometimes I want to cargo clean all projects on my system (see #11305).
Proposed Solution
We should track in the GC data base a list of
- Root-manifests (ie the
Cargo.toml/ cargo script associated with the target directory) - Target directory
- (maybe) The path to the
Cargo.lockfor future potential work like #13137 without having to infer theCargo.lock(special logic needed for cargo-script, feature requests exist for even weirder situations)
Note that neither of the two fields can serve as a unique / primary key. If people use CARGO_TARGET_DIR=/tmp/cargo then multiple workspaces may point to the same target dir. Likewise, people may end up with multiple target dirs for one workspace.
We need to track the Cargo.toml / cargo script because the workspace root is ambiguous when it comes to cargo scripts.
Example entries for CARGO_TARGET_DIR=/tmp/cargo :
| id | workspace-manifest | target dir | timestamp |
|---|---|---|---|
| ? | /foo/Cargo.toml | /tmp/cargo | ? |
| ? | /bar/Cargo.toml | /tmp/cargo | ? |
| ? | /baz/script.rs | /tmp/cargo | ? |
Example entries for rust-analyzer target dir
| id | workspace-manifest | target dir | timestamp |
|---|---|---|---|
| ? | /foo/Cargo.toml | /foo/target | ? |
| ? | /foo/Cargo.toml | /foo/target-ra | ? |
| ? | /bar/Cargo.toml | /bar/target | ? |
| ? | /bar/Cargo.toml | /bar/target-ra | ? |
| ? | /baz/script.rs | ~/.cargo/target/... | ? |
Forms of cleanup
- Delete
target/if unused for X time (this is in the "locally recreatable" category) - Delete all
target/(I just upgraded Rust, maybe rustup could suggest this) - Delete leaked
target/(workspace doesn't exist)- However, it might be transient (on a thumb drive). Should we make this time based?
Notes
No response
- Dominant language
- Rust
- Stars
- 15.5k
- Forks
- 3k
- Avg merge
- 23h 30m
- Merged PRs (30d)
- 51
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-lang/cargo
-
C-bug S-triage
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
A-completions C-bug S-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
C-bug S-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Command-fix
Difficulty 1/5 Under an hour Newbie friendliness 82/100
-
cargo remove prints "Removing X" for dependencies that never get removed when the command fails OpenC-bug Command-remove S-triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
enhancement remote
Difficulty 2/5 1-3 hours Newbie friendliness 68/100