If Rust builds are taking up too much disk space, using outdated dependencies, or behaving as though old files are still in use, clearing the Rust cache may help. For the Rust programming language, “cache” can mean several different things: compiled build files, downloaded crate packages, or Git-based dependencies. The right cleanup method depends on which one you want to remove. This guide explains how to clear each safely on Linux, macOS, and Windows, and what to leave alone.
What Does the Rust Cache Include?
Rust projects use Cargo, Rust’s package manager and build tool. Cargo keeps files in more than one place, so there is no single command that clears every kind of Rust cache. The main locations are:
-
Project build artifacts: Compiled files, incremental build data, and other outputs, usually stored in a project’s
targetdirectory. - Downloaded registry crates: Package archives and unpacked source code for dependencies from registries such as crates.io.
- Git dependencies: Cloned repositories and checkout data for dependencies loaded from Git.
-
Installed tools and Rust toolchains: Programs installed with
cargo installand Rust versions managed by rustup. These are not ordinary build caches.
Clearing one category does not automatically clear the others. For example, cargo clean removes project build outputs, but it does not remove downloaded crates from Cargo’s shared cache.
Quickest Way to Clear a Rust Project’s Build Cache
To remove compiled files for the project in your current directory, open a terminal in the project folder and run:
cargo clean
This removes the project’s target build directory, including compiled dependencies and build outputs. Cargo recreates the necessary files the next time you build or test the project. Your Rust source files, Cargo.toml, and Cargo.lock are not removed.
Use this command when a build seems stuck on stale artifacts, when you need to reclaim space from one project, or before checking whether an issue can be reproduced from a clean build. It is usually the safest first step because it affects the current project rather than Cargo’s shared dependency cache.
You can also clean a specific package or build configuration. For example, these commands target a named package or release build artifacts:
cargo clean --package my-package
cargo clean --release
Replace my-package with the package name from your Cargo manifest. If your project uses a custom target directory, an environment variable, or a Cargo configuration file, its build output may not be in a local target folder. Cargo’s target-dir setting and the CARGO_TARGET_DIR environment variable can change where those files are stored.
Clear Cargo’s Downloaded Crate and Git Caches
If your goal is to remove shared downloaded dependencies, find Cargo’s home directory. By default, this is ~/.cargo on Linux and macOS, or %USERPROFILE%\.cargo on Windows. If you set CARGO_HOME, Cargo uses that location instead.
Inside Cargo’s home directory, the registry folder contains registry indexes, downloaded crate archives, and unpacked crate sources. The git folder contains cached Git repositories and checkouts. Removing these directories can free space, but Cargo may need to download dependencies again the next time you build.
On Linux or macOS, first check whether you have a custom Cargo home, then remove the registry and Git cache folders:
echo "${CARGO_HOME:-$HOME/.cargo}"
rm -rf "${CARGO_HOME:-$HOME/.cargo}/registry"
rm -rf "${CARGO_HOME:-$HOME/.cargo}/git"
Review the path printed by the first command before running the removal commands. If you want to clear only one category, remove only registry or only git. Avoid copying a removal command without checking that it points to your Cargo directory.
In Windows PowerShell, you can use:
$CargoHome = if ($env:CARGO_HOME) { $env:CARGO_HOME } else { Join-Path $HOME ".cargo" }
$CargoHome
Remove-Item (Join-Path $CargoHome "registry") -Recurse -Force
Remove-Item (Join-Path $CargoHome "git") -Recurse -Force
The $CargoHome line displays the directory that the commands will use. Check that it is correct before deleting either folder. Once the cache is removed, Cargo will fetch missing registry packages and Git dependencies as needed. A network connection may be required, and the next build can take longer than usual.
How to Find Which Rust Cache Is Using Space
Before deleting anything, check where the space is going. Build artifacts are often the largest item when a project has been compiled many times or for multiple targets. Shared crate downloads can grow over time if you work on several Rust projects.
On Linux or macOS, you can inspect common locations with:
du -sh target
du -sh "${CARGO_HOME:-$HOME/.cargo}/registry"
du -sh "${CARGO_HOME:-$HOME/.cargo}/git"
A message that a directory does not exist is not necessarily a problem. You may be in the wrong project directory, may not have built the project yet, or may use a custom target or Cargo home directory.
In PowerShell, check the sizes of the common folders in File Explorer, or list their locations and inspect their properties. You can also use this command to show the default or configured Cargo home path:
if ($env:CARGO_HOME) { $env:CARGO_HOME } else { Join-Path $HOME ".cargo" }
For a quick, project-specific cleanup, cargo clean is simpler than manually removing folders. Use manual deletion when you specifically want to clear shared downloads or when a cache appears corrupted.
What Not to Delete When Clearing Rust Cache
Some important Rust files are easy to mistake for cache data. Avoid deleting them unless you intentionally want to change or uninstall something:
-
Cargo.lock: This file records the exact dependency versions selected for an application. Removing it can cause Cargo to resolve newer compatible versions and change the build. Libraries may handle lockfiles differently, but it is still not a cache folder. -
Cargo.toml: This manifest defines the package and its dependencies. It is essential project configuration. -
~/.rustup: This typically stores Rust toolchains and components managed by rustup. Deleting it can remove installed Rust versions, not just temporary build files. -
Installed binaries in
~/.cargo/bin: This directory commonly holds Rust tools installed withcargo install. Removing it can uninstall those tools. -
Project source and configuration: Do not remove your
srcdirectory or.cargo/config.tomlwhen trying to clear generated files.
If you want to remove an installed Rust toolchain, use rustup’s toolchain management commands rather than deleting rustup’s directory. If you want to uninstall a Cargo-installed program, use the appropriate package removal command when available, or remove only that program’s binary after confirming its name.
What to Do After Clearing the Cache
After running cargo clean, rebuild or test your project as usual:
cargo build
cargo test
After removing the shared registry or Git cache, run the same command you normally use. Cargo will download dependencies that are no longer present locally. For example, cargo check checks the project, while cargo build compiles it. The first run may be slower because Cargo has to retrieve and compile files again.
If the original problem continues after cleaning, check that you are building the intended package and target. Also check any environment variables or Cargo configuration that redirect build output. A cache cleanup cannot fix a missing dependency, a compiler error in source code, or a version conflict recorded in the project’s dependency configuration.
Clearing Rust Caches in Continuous Integration
Build pipelines often cache Cargo downloads or the target directory to make repeated jobs faster. If a CI build starts failing after a compiler, operating system, target, or dependency change, refreshing the pipeline cache may help. The exact steps depend on the CI provider, but typically involve changing the cache key or deleting the saved cache in the provider’s interface.
Use cache keys that account for relevant changes, such as the Rust toolchain version, operating system, target architecture, and dependency lockfile. Clearing a CI cache too often can make builds slower because every job must download or compile files again. For a one-time clean build, remove the relevant pipeline cache or run cargo clean in the job rather than wiping unrelated caches.
Frequently Asked Questions About Clearing Rust Cache
Does cargo clean delete my dependencies? It removes the current project’s compiled build artifacts. It does not normally delete Cargo’s shared downloaded crate sources or archives in the Cargo home directory.
Will clearing the cache break my project? It should not delete your project source or manifest when you use the commands described here. However, clearing shared downloads means Cargo may need internet access to fetch dependencies again. Keep a backup of important project files and review paths before using recursive deletion commands.
How often should I clear the Rust cache? There is no required schedule. Clear project build artifacts when you need disk space or want to rule out stale outputs. Clear shared registry or Git caches only when you have a reason, such as recovering disk space or troubleshooting suspected corruption.
Does deleting the cache make Rust run faster? Usually not. The cache exists to avoid repeated downloads and compilation. Clearing it can help resolve certain stale-build problems, but the next build will commonly take longer while Cargo recreates the files.
Conclusion
To clear a Rust cache, first decide which files you mean. Run cargo clean to remove build artifacts for the current project. To clear shared downloaded crates or Git dependencies, remove the corresponding registry or git folders inside the configured Cargo home directory, after verifying the path. Leave Cargo.lock, project files, rustup toolchains, and installed tools alone unless you specifically intend to change them. These steps let you free space or troubleshoot stale builds without removing more than necessary.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.