Last active
April 4, 2026 01:32
-
-
Save bramp/ca45fe6b64fbc7ff398022dbc2e35eb7 to your computer and use it in GitHub Desktop.
macOS safe cleanup script (30-day grace period)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Author: Andrew Brampton (bramp.net) | |
| # Link: https://gist.github.com/bramp/ca45fe6b64fbc7ff398022dbc2e35eb7 | |
| # safe_cleanup.sh - Reclaims disk space by clearing files older than 30 days. | |
| DAYS=30 | |
| show_space() { | |
| echo "Filesystem Size Used Avail Capacity Mounted on" | |
| df -h / /System/Volumes/Data | tail -n +2 | |
| } | |
| echo "--- Starting 30-Day Grace Period Cleanup ---" | |
| echo "" | |
| echo "--- Before Cleanup ---" | |
| show_space | |
| echo "" | |
| # 1. Homebrew Cleanup | |
| if command -v brew &> /dev/null; then | |
| echo "[1/6] Cleaning up old Homebrew versions and stale lockfiles..." | |
| brew cleanup -s &> /dev/null | |
| fi | |
| # 2. Docker (Prune objects older than 30 days) | |
| if command -v docker &> /dev/null; then | |
| echo "[2/6] Pruning Docker objects older than ${DAYS} days (if daemon is running)..." | |
| # We attempt the prune but silence errors in case the daemon is stopped | |
| docker system prune -f --filter "until=$((DAYS*24))h" &> /dev/null | |
| fi | |
| # 3. Development Caches (Pants, Pre-commit) | |
| echo "[3/6] Cleaning development caches (Pants, Pre-commit) older than ${DAYS} days..." | |
| [ -d "$HOME/.cache/pre-commit" ] && find "$HOME/.cache/pre-commit" -mindepth 1 -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| [ -d "$HOME/.cache/pants" ] && find "$HOME/.cache/pants" -mindepth 1 -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| # 4. macOS User Caches (Google, Sublime Text, etc.) | |
| echo "[4/6] Cleaning Library caches older than ${DAYS} days..." | |
| [ -d "$HOME/Library/Caches/Google" ] && find "$HOME/Library/Caches/Google" -mindepth 1 -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| [ -d "$HOME/Library/Caches/Sublime Text" ] && find "$HOME/Library/Caches/Sublime Text" -mindepth 1 -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| # 5. Generic Cache Folders (Apply 30-day rule to any other subfolders in ~/.cache) | |
| echo "[5/6] Cleaning generic ~/.cache subdirectories older than ${DAYS} days..." | |
| find "$HOME/.cache" -mindepth 1 -maxdepth 1 -type d -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| # 6. Tool-specific caches (pip, npm, go) | |
| echo "[6/6] Cleaning tool-specific cache directories (pip, npm, go) older than ${DAYS} days..." | |
| [ -d "$HOME/Library/Caches/pip" ] && find "$HOME/Library/Caches/pip" -mindepth 1 -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| [ -d "$HOME/.npm/_cacache" ] && find "$HOME/.npm/_cacache" -mindepth 1 -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| if [ -d "$HOME/Library/Caches/go-build" ]; then | |
| find "$HOME/Library/Caches/go-build" -mindepth 1 -mtime +$DAYS -exec rm -rf {} + 2>/dev/null | |
| fi | |
| echo "" | |
| echo "--- Cleanup Complete! ---" | |
| echo "" | |
| echo "--- After Cleanup ---" | |
| show_space |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment