Prune

Dev tools

Rust eating your disk: target folders and the Cargo cache

Last checked Applies to Rust projects built with Cargo and toolchains installed with rustup, on macOS and Windows 6 min read

Short answer

Most of the space is usually in each project's target folder, not in ~/.cargo. Run cargo clean in projects you aren't working on, and Cargo rebuilds everything on the next build. The download cache in ~/.cargo/registry on Mac or C:\Users\<USERNAME>\.cargo\registry on Windows can go too, but keep the rest of .cargo, because bin holds cargo and rustup themselves.

Build output
A target folder in the root of every project or workspace
Cache on Mac
~/.cargo/registry and ~/.cargo/git
Cache on Windows
C:\Users\<USERNAME>\.cargo\registry and ...\.cargo\git
Moved it?
CARGO_HOME moves the cache, CARGO_TARGET_DIR moves build output
Clean command
cargo clean, or cargo clean --dry-run to preview
Auto cleanup
From Rust 1.88, Cargo deletes unused cache files: downloads after 3 months, local files after 1 month

Two places Rust uses space

Rust uses disk space in two places, and they don't behave the same way.

  • The target folder. Every project or workspace gets one in its root. It holds compiled dependencies, incremental build data and your binaries, separately for debug and release builds. It's per project, so ten Rust projects means ten of them, and Cargo never cleans them for you.
  • The Cargo home. By default that's ~/.cargo on Mac and C:\Users\<USERNAME>\.cargo on Windows, or wherever CARGO_HOME points. Every project shares it.

Inside the Cargo home, per the Cargo Book:

  • registry/cache holds downloaded crates as compressed .crate files.
  • registry/src holds the same crates unpacked, so every dependency is on disk twice.
  • registry/index holds the crates.io metadata.
  • git/db and git/checkouts hold dependencies that come from git repositories.
  • bin holds cargo, rustc and rustup themselves, plus anything you installed with cargo install.
  • config.toml and credentials.toml are your settings and registry login. They aren't cache.

Cargo now cleans its own cache

Many older answers say the Cargo cache grows forever. That's no longer true since Rust 1.88. Cargo now deletes files in its home folder that haven't been used for a while: files downloaded from the network after 3 months, and files it can rebuild locally, like the unpacked sources, after 1 month. It checks at most once a day, and only while running commands that already do real work, such as cargo build, cargo test, cargo check or cargo fetch. It skips the cleanup when you run offline with --offline or --frozen.

You can change how often it checks with cache.auto-clean-frequency in config.toml. It takes "never", "always" or a time such as "1 day", which is the default. The setting changes how often Cargo checks, not how old files have to be.

This cleanup never touches target folders. That's usually where most of your Rust disk space went.

Free the space in four steps

  1. Measure the Cargo home

    du -sh ~/.cargo/registry/* ~/.cargo/git/*
    # PowerShell
    Get-ChildItem "$env:USERPROFILE\.cargo\registry", "$env:USERPROFILE\.cargo\git" -Directory | ForEach-Object {
      $gb = (Get-ChildItem $_.FullName -Recurse -File | Measure-Object Length -Sum).Sum / 1GB
      "{0,8:N2} GB  {1}" -f $gb, $_.FullName
    }

    Then do the same for the target folder of each project you have on disk.

  2. Preview a project clean

    cargo clean --dry-run

    Run it in the project folder. It shows what it would delete and doesn't delete anything. It works the same on Mac and Windows.

  3. Clean the projects you aren't building

    # everything in target
    cargo clean
    # only release builds
    cargo clean --release
    # only generated docs
    cargo clean --doc

    Your next build compiles every dependency again, so it'll take longer once. If you set CARGO_TARGET_DIR or build.target-dir, your build output lives in that folder instead of the project.

  4. Trim the download cache

    rm -rf ~/.cargo/registry/src ~/.cargo/registry/cache ~/.cargo/git/checkouts
    # PowerShell
    Remove-Item -Recurse -Force "$env:USERPROFILE\.cargo\registry\src", "$env:USERPROFILE\.cargo\registry\cache", "$env:USERPROFILE\.cargo\git\checkouts"

    Close your editor and stop any running builds first. Cargo unpacks or downloads crates again the next time a project needs them, so that build will need the network.

SafeRunning cargo clean in a project, or deleting registry/src. Cargo rebuilds the first and unpacks the second again from registry/cache without the network.

CarefulDeleting registry/cache, registry/index or git/db. Cargo downloads them again, so your next build needs the network, and offline builds fail until it has.

Do notdelete the whole .cargo folder. bin holds cargo and rustup, so the commands stop working, and you lose config.toml, credentials.toml and every tool you added with cargo install.

Old toolchains take space too. rustup toolchain list shows the ones you have, and rustup toolchain uninstall <name> removes one you no longer use.

A target folder is Rust's version of node_modules: one per project, easy to forget. Deleting old node_modules covers the JavaScript side.

FAQ

Is it safe to delete the target folder in a Rust project?

Yes. It only holds build output. cargo clean deletes it, and the next build compiles everything again, which takes longer once. Your source code isn't in it.

Why is my .cargo folder still big after Rust 1.88?

Cargo only deletes cache files that haven't been used for 3 months, or 1 month for files it can rebuild locally. Crates your projects used recently stay. The cleanup also never runs during offline builds.

Can I move the Cargo cache to another drive?

Yes. Set the CARGO_HOME environment variable to a folder on the other drive. It works the same on Mac and Windows. Move bin, config.toml and credentials.toml with it, because they live in the same folder, and point your PATH at the new bin folder.

Can all my projects share one target folder?

Yes. Set CARGO_TARGET_DIR, or build.target-dir in your Cargo config, and every project builds into that one folder. Then there's only one folder to clean.