Created
June 10, 2025 19:50
-
-
Save ehzawad/01d8d6539a64351c6b50c5e05551f415 to your computer and use it in GitHub Desktop.
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 | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
BLUE='\033[0;34m' | |
BOLD='\033[1m' | |
NC='\033[0m' | |
get_size() { | |
if [ -d "$1" ]; then | |
du -sh "$1" 2>/dev/null | cut -f1 || echo "0B" | |
else | |
echo "0B" | |
fi | |
} | |
delete_with_progress() { | |
local path="$1" | |
local name="$2" | |
if [ -d "$path" ] || [ -f "$path" ]; then | |
local size_before=$(get_size "$path") | |
echo -n "ποΈ Deleting $name ($size_before)... " | |
if rm -rf "$path"/* 2>/dev/null; then | |
echo -e "${GREEN}β Done${NC}" | |
return 0 | |
else | |
echo -e "${RED}β Failed${NC}" | |
return 1 | |
fi | |
else | |
echo -e "β οΈ $name not found - ${YELLOW}skipping${NC}" | |
return 0 | |
fi | |
} | |
delete_files() { | |
local path="$1" | |
local name="$2" | |
if [ -d "$path" ]; then | |
local count=$(find "$path" -type f 2>/dev/null | wc -l | tr -d ' ') | |
if [ $count -gt 0 ]; then | |
echo -n "ποΈ Deleting $count $name files... " | |
if rm -rf "$path"/* 2>/dev/null; then | |
echo -e "${GREEN}β Done${NC}" | |
return 0 | |
else | |
echo -e "${RED}β Failed${NC}" | |
return 1 | |
fi | |
else | |
echo -e "βΉοΈ No $name to delete" | |
return 0 | |
fi | |
else | |
echo -e "β οΈ $name directory not found" | |
return 0 | |
fi | |
} | |
calculate_space_recovered() { | |
echo -e "\n${BLUE}${BOLD}π CALCULATING SPACE RECOVERED...${NC}" | |
local total_before=0 | |
local total_after=0 | |
# Check remaining sizes | |
local dirs_to_check=( | |
"~/Library/Caches/Google" | |
"~/Library/Caches/pip" | |
"~/Library/Caches/Dia" | |
"~/Library/Caches/SiriTTS" | |
"~/Library/Caches/icloudmailagent" | |
"~/Library/Caches/company.thebrowser.dia" | |
"~/Library/Caches/ms-playwright-go" | |
"~/Library/Caches/node-gyp" | |
"~/Library/Caches/Homebrew" | |
"~/Library/Logs/DiagnosticReports" | |
) | |
echo "Checking remaining cache sizes..." | |
for dir in "${dirs_to_check[@]}"; do | |
expanded_dir=$(eval echo "$dir") | |
if [ -d "$expanded_dir" ]; then | |
local remaining=$(get_size "$expanded_dir") | |
echo " $(basename "$expanded_dir"): $remaining remaining" | |
fi | |
done | |
} | |
echo -e "${GREEN}${BOLD}π§Ή macOS CLEANUP DELETION SCRIPT${NC}" | |
echo "==================================" | |
echo "" | |
echo -e "${YELLOW}${BOLD}READY TO DELETE:${NC}" | |
echo "β’ Google Chrome cache: 1.5G" | |
echo "β’ Python pip cache: 976M" | |
echo "β’ Dia cache: 710M" | |
echo "β’ Siri TTS cache: 444M" | |
echo "β’ iCloud mail agent cache: 313M" | |
echo "β’ Browser Dia cache: 139M" | |
echo "β’ Playwright cache: 127M" | |
echo "β’ node-gyp cache: 106M" | |
echo "β’ Homebrew cache: 73M" | |
echo "β’ Diagnostic reports: 27 files" | |
echo "β’ Various smaller caches" | |
echo "" | |
echo -e "${BOLD}Estimated space recovery: ~4.5GB${NC}" | |
echo "" | |
echo -e "${RED}${BOLD}β οΈ FINAL WARNING:${NC}" | |
echo "β’ This will permanently delete cache files" | |
echo "β’ Applications may start slower initially" | |
echo "β’ Caches will regenerate as needed" | |
echo "β’ Close all applications first" | |
echo "" | |
read -p "π€ Are you sure you want to proceed? (type 'yes' to continue): " confirm | |
if [ "$confirm" != "yes" ]; then | |
echo -e "${YELLOW}Operation cancelled.${NC}" | |
exit 0 | |
fi | |
echo "" | |
echo -e "${GREEN}${BOLD}π STARTING CLEANUP...${NC}" | |
echo "" | |
# Track successful deletions | |
deleted_count=0 | |
failed_count=0 | |
# Delete largest caches first | |
echo -e "${BLUE}${BOLD}Phase 1: Large Application Caches${NC}" | |
delete_with_progress "~/Library/Caches/Google" "Google Chrome cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/pip" "Python pip cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/Dia" "Dia cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/SiriTTS" "Siri TTS cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/icloudmailagent" "iCloud mail agent cache" && ((deleted_count++)) || ((failed_count++)) | |
echo "" | |
echo -e "${BLUE}${BOLD}Phase 2: Development Caches${NC}" | |
delete_with_progress "~/Library/Caches/company.thebrowser.dia" "Browser Dia cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/ms-playwright-go" "Playwright cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/node-gyp" "node-gyp cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/Homebrew" "Homebrew cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/typescript" "TypeScript cache" && ((deleted_count++)) || ((failed_count++)) | |
echo "" | |
echo -e "${BLUE}${BOLD}Phase 3: System Caches${NC}" | |
delete_with_progress "~/Library/Caches/com.colliderli.iina" "IINA cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/com.apple.Music" "Apple Music cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/us.zoom.xos" "Zoom cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/com.openai.chat" "ChatGPT cache" && ((deleted_count++)) || ((failed_count++)) | |
echo "" | |
echo -e "${BLUE}${BOLD}Phase 4: Browser Data${NC}" | |
delete_with_progress "~/Library/Safari/LocalStorage" "Safari Local Storage" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/WebKit/NetworkProcess" "WebKit Network Cache" && ((deleted_count++)) || ((failed_count++)) | |
delete_with_progress "~/Library/Caches/com.apple.Safari" "Safari cache" && ((deleted_count++)) || ((failed_count++)) | |
echo "" | |
echo -e "${BLUE}${BOLD}Phase 5: Logs and Reports${NC}" | |
delete_files "~/Library/Logs/DiagnosticReports" "diagnostic report" && ((deleted_count++)) || ((failed_count++)) | |
echo "" | |
echo -e "${BLUE}${BOLD}Phase 6: Trash${NC}" | |
delete_files "~/.Trash" "trash" && ((deleted_count++)) || ((failed_count++)) | |
echo "" | |
echo -e "${YELLOW}${BOLD}System-level cleanup (requires sudo):${NC}" | |
read -p "π Delete system caches? This requires admin password (y/N): " system_confirm | |
if [ "$system_confirm" = "y" ] || [ "$system_confirm" = "Y" ]; then | |
echo "" | |
echo -e "${BLUE}${BOLD}Phase 7: System Caches (sudo required)${NC}" | |
if sudo -n true 2>/dev/null || sudo echo "Admin access granted..." >/dev/null 2>&1; then | |
echo -n "ποΈ Deleting system icon cache (190M)... " | |
if sudo rm -rf /Library/Caches/com.apple.iconservices.store/* 2>/dev/null; then | |
echo -e "${GREEN}β Done${NC}" | |
((deleted_count++)) | |
else | |
echo -e "${RED}β Failed${NC}" | |
((failed_count++)) | |
fi | |
echo -n "ποΈ Deleting system diagnostic reports... " | |
if sudo rm -rf /Library/Logs/DiagnosticReports/* 2>/dev/null; then | |
echo -e "${GREEN}β Done${NC}" | |
((deleted_count++)) | |
else | |
echo -e "${RED}β Failed${NC}" | |
((failed_count++)) | |
fi | |
echo -n "ποΈ Clearing font caches... " | |
if sudo atsutil databases -remove 2>/dev/null; then | |
echo -e "${GREEN}β Done${NC}" | |
((deleted_count++)) | |
else | |
echo -e "${YELLOW}β οΈ Partial${NC}" | |
fi | |
else | |
echo -e "${RED}β Could not get admin access${NC}" | |
fi | |
else | |
echo -e "${YELLOW}Skipping system-level cleanup${NC}" | |
fi | |
calculate_space_recovered | |
echo "" | |
echo -e "${GREEN}${BOLD}π CLEANUP COMPLETE!${NC}" | |
echo "==================================" | |
echo -e "β Successfully cleaned: ${GREEN}$deleted_count items${NC}" | |
if [ $failed_count -gt 0 ]; then | |
echo -e "β Failed to clean: ${RED}$failed_count items${NC}" | |
fi | |
echo "" | |
echo -e "${BLUE}${BOLD}What happens next:${NC}" | |
echo "β’ Applications will regenerate caches as needed" | |
echo "β’ First launch of apps may be slightly slower" | |
echo "β’ System performance should improve" | |
echo "β’ Run this script again anytime to clean up new caches" | |
echo "" | |
echo -e "${GREEN}${BOLD}You've successfully reclaimed several GB of disk space! π${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment