Skip to content

Instantly share code, notes, and snippets.

@ShabbirHasan1
Forked from alexheretic/clean-dot-cargo
Created June 4, 2026 19:52
Show Gist options
  • Select an option

  • Save ShabbirHasan1/fc01e4bc4d335e11db4cb5541387bcfe to your computer and use it in GitHub Desktop.

Select an option

Save ShabbirHasan1/fc01e4bc4d335e11db4cb5541387bcfe to your computer and use it in GitHub Desktop.
Deletes ~/.cargo cached data older than 2 weeks
#!/usr/bin/env bash
## Deletes ~/.cargo cached data older than 2 weeks
set -eu
du_before=$(du -hd0 ~/.cargo/ | cut -f1)
# remove old crate caches & sources
find ~/.cargo/registry/cache/ -type f -ctime +13 -atime +13 -delete
find ~/.cargo/registry/src/ -type f -ctime +13 -atime +13 \
| cut -d'/' -f1-8 | sort | uniq | grep . \
| xargs -n1 rm -rf
# remove old git checkouts
find ~/.cargo/git -type f -ctime +13 -atime +13 \
| cut -d'/' -f1-7 | sort | uniq | grep . \
| xargs -n1 rm -rf
du_after=$(du -hd0 ~/.cargo/ | cut -f1)
if [ "$du_after" != "$du_before" ]; then
# shellcheck disable=SC2088
echo "~/.cargo: $du_before -> $du_after"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment