cargo clean: Usage, Options & Common CI Errors
Delete build artifacts to free disk space.
cargo clean removes the target directory (or part of it). It is useful for reclaiming disk on a full runner, but used carelessly it throws away the incremental cache you want to keep.
What it does
Deletes target/ by default. Scopes let you remove only the release profile, only a specific package, or only artifacts for a given target triple.
Common usage
cargo clean # remove all of target/
cargo clean --release # only target/release
cargo clean -p mycrate # only this package’s artifacts
cargo clean --target x86_64-unknown-linux-muslCommon CI error: clean kills the cache
A "no space left on device" fix that runs cargo clean before the build wipes the cached target/ you just restored, so every CI run recompiles from scratch and gets slower. Fix: clean selectively (cargo clean -p <crate> or --release), or cache a pruned target/ rather than cleaning the whole thing.
Options
| Flag | Effect |
|---|---|
| --release | Clean only the release profile |
| -p <pkg> | Clean one package |
| --target <triple> | Clean one target’s artifacts |
| --doc | Clean only the doc output |