LinuxCommandLibrary

cargo-clean

Remove compiled artifacts

TLDR

Remove the entire target directory

$ cargo clean
copy

Remove documentation artifacts (the target/doc directory)
$ cargo clean --doc
copy

Remove release artifacts (the target/release directory)
$ cargo clean [[-r|--release]]
copy

Remove artifacts in the directory of the given profile (in this case, target/debug)
$ cargo clean --profile [dev]
copy

SYNOPSIS

cargo clean [OPTIONS]

PARAMETERS

-p , --package
    Clean only the specified package's artifacts within the workspace.

--doc
    Clean only the target/doc directory, where documentation generated by cargo doc resides.

--release
    Clean artifacts from the target/release directory (optimized build) instead of the default target/debug directory.

--profile
    Clean artifacts for a specific build profile (e.g., dev, release, or a custom profile).

--target
    Clean artifacts only for the specified target triple (e.g., x86_64-unknown-linux-gnu).

--target-dir


    Specify the directory where all generated artifacts are stored. Defaults to target relative to the workspace root.

-q, --quiet
    Do not print Cargo's `clean` output messages.

-v, --verbose
    Use verbose output, printing more information during the cleaning process.

--color
    Control when colors are used in output. Possible values: auto, always, never.

-h, --help
    Prints help information about the command.

DESCRIPTION

The cargo clean command is used to remove the target directory, which contains all compiled artifacts generated by Rust's build system (Cargo). This includes executables, libraries, intermediate object files, and documentation generated by cargo doc. Its primary purposes are to free up disk space, ensure a clean build from scratch for troubleshooting, or to prepare a project for distribution without unnecessary compiled files. Running cargo clean ensures that the next build will be a full recompilation, which can resolve issues caused by stale or corrupted build caches.

CAVEATS

Using cargo clean deletes all compiled outputs, meaning the next build will require a full recompilation of your project and its dependencies, which can be time-consuming. It does not clean Cargo's global caches (e.g., downloaded crates in ~/.cargo/registry or git checkouts in ~/.cargo/git), nor does it remove files from the .cargo/bin directory for installed binaries.

WHEN TO USE

cargo clean is particularly useful in several scenarios:
- When troubleshooting persistent build errors that might be due to stale artifacts.
- To free up disk space, as compiled projects, especially with multiple build profiles or targets, can consume significant storage.
- Before distributing a project or committing to version control, to ensure only source files are included.
- In Continuous Integration/Continuous Deployment (CI/CD) pipelines to guarantee a fresh build environment for each job.

PARTIAL CLEANING

While cargo clean by default removes the entire target directory, options like --doc, --release, --profile, and --target allow for more granular control, enabling you to clean only specific subsets of artifacts without deleting everything. This is useful if you only want to clear, for instance, release builds without affecting your debug development flow.

HISTORY

Cargo, the Rust package manager and build system, was introduced early in Rust's development (around 2014) to standardize project management, dependency resolution, and compilation. The cargo clean command has been a fundamental utility within Cargo from its inception, providing a straightforward and integrated way to manage build artifacts and maintain a tidy development environment.

SEE ALSO

cargo build(1), cargo run(1), cargo test(1), cargo check(1), rm(1)

Copied to clipboard