Skip to content

Instantly share code, notes, and snippets.

@arHSM
Last active December 26, 2023 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arHSM/cca218aee11d5e3e50419214ac0a99a3 to your computer and use it in GitHub Desktop.
Save arHSM/cca218aee11d5e3e50419214ac0a99a3 to your computer and use it in GitHub Desktop.
Clean cargo registry
#!/usr/bin/env bash
## Deletes ~/.cargo cached data older than 2 weeks or `OFFLOAD_THRESHOLD`-days
# Based off of:
# https://gist.github.com/alexheretic/661cb78b49ca280c629c64be0fabf8d2
# Changes: added brainrot_location & threshold variable
# I reccomend pairing this with cargo-sweep https://github.com/holmgr/cargo-sweep
set -eu
# accept optional location for cargo registry
# so i can clean my brainrot on windows from wsl!
brainrot_location=${1:-"~/.cargo/"}
# amount of time to consider when keeping files
# read from `OFFLOAD_THRESHOLD` env var
# 13 was the default in the original script
threshold="+${OFFLOAD_THRESHOLD:-"13"}"
du_before=$(du -hd0 $brainrot_location | cut -f1)
# remove old crate caches & sources
find "${brainrot_location}/registry/cache/" -type f -ctime $threshold -atime $threshold -delete
find "${brainrot_location}/registry/src/" -type f -ctime $threshold -atime $threshold \
| cut -d'/' -f1-8 | sort | uniq | grep . \
| xargs -n1 rm -rf
# remove old git checkouts
find "${brainrot_location}/git" -type f -ctime $threshold -atime $threshold \
| cut -d'/' -f1-7 | sort | uniq | grep . \
| xargs -n1 rm -rf
du_after=$(du -hd0 $brainrot_location | cut -f1)
if [ "$du_after" != "$du_before" ]; then
# shellcheck disable=SC2088
echo "$brainrot_location: $du_before -> $du_after"
else
echo "unchanged, congratulations no brainrot!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment