Why is the ~/.cargo directory so big?
Asked Answered
D

2

11

On my Windows 10 machine it's 3.5GB. What is it storing? How can I trim it down?

Deuteragonist answered 25/9, 2020 at 12:4 Comment(2)
My intermediate directory of our C++ build has about 150GB ... so 3.5 doesn't seem to bad for whatever build system :-)Hydrophilic
@MartinBa That includes object files and debugging information for them. .cargo does not include build artifacts for a given project.Jebel
J
15

It is storing all the downloaded crates you have used, their binaries, the index of registries, etc. It is going to take a lot of space and will keep increasing.

You can safely remove .cargo/registry/ (if you remove the entire folder, you will lose installed binaries and your Cargo configuration if you had one). Afterwards, everything you use again will be downloaded and it will start growing back. It is a common way of getting rid of very old dependencies you are not using anymore.

Jebel answered 25/9, 2020 at 12:10 Comment(8)
It also contains every version of the crate you've downloaded btw.Vermont
I definitely wouldn't remove anything but ~/.cargo/registry/ (assuming Windows has the same folder hierarchy as Linux). The rest are the binaries you've installed (you probably don't want to remove those) and the configuration (that's tiny). The registry is the one big thing, it can safely be removed, and will be reconstructed as needed.Erumpent
I was under the impression downloaded crates went into the target directory of each project?Deuteragonist
@PabloTatoRamos the compiled artifacts do, the sources are shared. The reason is that sources are the same for every project, while artifacts might be different due to different compiler options, different features sets, etc.Erumpent
@Erumpent Yeah, let's say that.Jebel
@PabloTatoRamos they are copied into the target directory (or perhaps they're complied in place but the target directory for their output is the target directory for your crate, I'm not 100% sure), however the actual code for them is stored in ~/.cargo. This would actually save some space if you have multiple projects which use the same dependencies.Vermont
.cargo/registry has 3 subdirectories, I'd there's probably no need to remove index (which would be the cargo index) and cache is just the crate data so may or may not be useful to clean up.Ludovico
This might be a noob question but if I vendor all my dependencies into my project folder first, and then delete the registry folder, can I build the project totally offline thereafter?Straightforward
S
1

You can now keep the directory smaller on nightly, if you set in .cargo/config.toml (for example, ~/.cargo/config.toml or %USERPROFILE%\.cargo\config.toml:

[unstable]
gc = true

Then, Cargo commands will check once a day for crates that weren't used for a long time, and delete them from the cache.

For more information and additional configuration, see the blog post introducing the feature and the feature's section in the Cargo book.

Synthetic answered 29/2 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.