Cleaning target folders with cargo sweep

Hey everyone

I realized that my backup memory consumption has been growing since last March (See image). When searching for the cause I realized that target folders from rust projects were taking up 30GiB of physical memory. Apparently cargo doesn't clean up old build artifacts and every new version of the compiler creates new build artifacts for all dependencies. Therefore, there is a lot of garbage floating around in these target folders. I found a nice cargo tool called cargo-sweep. With it you can delete those unneeded build artifacts. It even allows for recursive search. So I did a

cargo sweep -r -i $HOME

This cleans all artifacts for toolchains that aren't installed anymore. It deleted almost 20GiB of physical memory from my system! I thought I'd give you a heads up so you can do the same if you need the space :slight_smile:.

Btw, I am not saying that cargo's behavior is wrong. Just that the occasional clean up could make a lot of sense. I also excluded target folder from my backups now.

3 Likes

On Bash with globstar option, I usually run

for f in **/Cargo.toml; do cargo clean --manifest-path $f; done

It should do something very similar, for those who want a more built-in approach.

3 Likes

Yes, except it will delete all build files instead of only outdated ones.
If you wanna do the same with cargo sweep you need

cargo sweep -r --maxsize=0 $HOME
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.