Created
April 8, 2025 03:32
-
-
Save razhangwei/2012d8c250d091ffaad1bc935f49c581 to your computer and use it in GitHub Desktop.
Clean up common coding related cache (uv, huggingface, npm, homebrew)
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 | |
# === Cache Cleanup Script === | |
# Cleans caches for Homebrew, UV, Hugging Face, and NPM | |
# Usage: bash cleanup.sh [options] | |
# Options: | |
# -a, --all Clean all caches | |
# -b, --brew Clean Homebrew cache | |
# -u, --uv Clean UV cache | |
# -h, --huggingface Clean Hugging Face cache | |
# -n, --npm Clean NPM cache | |
# --dry-run Show what would be cleaned without removing files | |
set -e | |
# Colors for output | |
GREEN='\033[0;32m' | |
BLUE='\033[0;34m' | |
YELLOW='\033[1;33m' | |
RED='\033[0;31m' | |
NC='\033[0m' # No Color | |
# Default settings | |
CLEAN_BREW=false | |
CLEAN_UV=false | |
CLEAN_HF=false | |
CLEAN_NPM=false | |
DRY_RUN=false | |
# Function to print header | |
print_header() { | |
echo | |
echo -e "${BLUE}===== $1 =====${NC}" | |
echo | |
} | |
# Function to print sizes | |
print_size() { | |
if [ -d "$1" ]; then | |
SIZE=$(du -sh "$1" 2>/dev/null | cut -f1) | |
echo -e "${YELLOW}Current size of $2: $SIZE${NC}" | |
else | |
echo -e "${YELLOW}$2 directory not found: $1${NC}" | |
fi | |
} | |
# Function to print summary | |
print_summary() { | |
echo -e "${GREEN}$1${NC}" | |
} | |
# Parse command line arguments | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
-a|--all) | |
CLEAN_BREW=true | |
CLEAN_UV=true | |
CLEAN_HF=true | |
CLEAN_NPM=true | |
shift | |
;; | |
-b|--brew) | |
CLEAN_BREW=true | |
shift | |
;; | |
-u|--uv) | |
CLEAN_UV=true | |
shift | |
;; | |
-h|--huggingface) | |
CLEAN_HF=true | |
shift | |
;; | |
-n|--npm) | |
CLEAN_NPM=true | |
shift | |
;; | |
--dry-run) | |
DRY_RUN=true | |
shift | |
;; | |
*) | |
echo "Unknown option: $1" | |
exit 1 | |
;; | |
esac | |
done | |
# If no specific cache was selected, default to all | |
if ! $CLEAN_BREW && ! $CLEAN_UV && ! $CLEAN_HF && ! $CLEAN_NPM; then | |
CLEAN_BREW=true | |
CLEAN_UV=true | |
CLEAN_HF=true | |
CLEAN_NPM=true | |
fi | |
if $DRY_RUN; then | |
echo -e "${YELLOW}Running in dry-run mode. No files will be deleted.${NC}" | |
fi | |
# ======== Homebrew Cleanup ======== | |
if $CLEAN_BREW; then | |
print_header "Homebrew Cleanup" | |
# Check if Homebrew is installed | |
if ! command -v brew >/dev/null 2>&1; then | |
echo -e "${RED}Homebrew is not installed.${NC}" | |
else | |
# Get brew cache directory | |
BREW_CACHE=$(brew --cache) | |
print_size "$BREW_CACHE" "Homebrew cache" | |
echo "Updating Homebrew..." | |
brew update | |
if $DRY_RUN; then | |
echo "Would clean up old versions with: brew cleanup -n" | |
brew cleanup -n | |
else | |
echo "Cleaning up old versions..." | |
brew cleanup --prune=all | |
echo "Removing download cache..." | |
rm -rf "$(brew --cache)" | |
fi | |
print_summary "Homebrew cleanup completed!" | |
fi | |
fi | |
# ======== UV Cleanup ======== | |
if $CLEAN_UV; then | |
print_header "UV Cleanup" | |
# Check if UV is installed | |
if ! command -v uv >/dev/null 2>&1; then | |
echo -e "${RED}UV is not installed.${NC}" | |
else | |
# Get UV cache directory | |
UV_CACHE=$(uv cache dir 2>/dev/null || echo "$HOME/.cache/uv") | |
print_size "$UV_CACHE" "UV cache" | |
if $DRY_RUN; then | |
echo "Would clean UV cache with: uv cache clean" | |
else | |
echo "Cleaning UV cache..." | |
uv cache clean | |
fi | |
print_summary "UV cleanup completed!" | |
fi | |
fi | |
# ======== Hugging Face Cleanup ======== | |
if $CLEAN_HF; then | |
print_header "Hugging Face Cleanup" | |
# Hugging Face cache directories | |
HF_CACHE="$HOME/.cache/huggingface" | |
print_size "$HF_CACHE" "Hugging Face cache" | |
if [ -d "$HF_CACHE" ]; then | |
if $DRY_RUN; then | |
echo "Would remove Hugging Face cache files from: $HF_CACHE" | |
else | |
echo "Cleaning Hugging Face cache..." | |
rm -rf "$HF_CACHE/hub" | |
rm -rf "$HF_CACHE/modules" | |
rm -rf "$HF_CACHE/datasets" | |
# Create .no_exists files to maintain directory structure but prevent large downloads | |
mkdir -p "$HF_CACHE/hub" | |
touch "$HF_CACHE/hub/.no_exists" | |
mkdir -p "$HF_CACHE/modules" | |
touch "$HF_CACHE/modules/.no_exists" | |
mkdir -p "$HF_CACHE/datasets" | |
touch "$HF_CACHE/datasets/.no_exists" | |
fi | |
print_summary "Hugging Face cleanup completed!" | |
else | |
echo -e "${YELLOW}No Hugging Face cache found.${NC}" | |
fi | |
fi | |
# ======== NPM Cleanup ======== | |
if $CLEAN_NPM; then | |
print_header "NPM Cleanup" | |
# Check if NPM is installed | |
if ! command -v npm >/dev/null 2>&1; then | |
echo -e "${RED}NPM is not installed.${NC}" | |
else | |
# Get NPM cache directory | |
NPM_CACHE=$(npm config get cache) | |
print_size "$NPM_CACHE" "NPM cache" | |
if $DRY_RUN; then | |
echo "Would clean NPM cache with: npm cache clean --force" | |
else | |
echo "Cleaning NPM cache..." | |
npm cache clean --force | |
echo "Finding large node_modules directories..." | |
find "$HOME/Projects" -name "node_modules" -type d -prune -exec du -sh {} \; 2>/dev/null | sort -hr | head -10 | |
echo -e "${YELLOW}Consider removing node_modules from projects you're no longer working on${NC}" | |
fi | |
print_summary "NPM cleanup completed!" | |
fi | |
fi | |
# Final summary | |
echo | |
echo -e "${GREEN}=====================================${NC}" | |
echo -e "${GREEN}Cache cleanup operations completed!${NC}" | |
echo -e "${GREEN}=====================================${NC}" | |
echo | |
echo "To see current disk space: df -h" | |
echo "To see large directories: du -sh /* 2>/dev/null | sort -hr | head -10" | |
echo | |
echo "Run this script with --dry-run to see what would be cleaned without removing files." | |
# Exit cleanly | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment