Skip to content

Instantly share code, notes, and snippets.

@ronnycoding
Last active September 20, 2024 22:16
Show Gist options
  • Save ronnycoding/4be2f9681bddf1794abc5c91e86cac3e to your computer and use it in GitHub Desktop.
Save ronnycoding/4be2f9681bddf1794abc5c91e86cac3e to your computer and use it in GitHub Desktop.
Dynamic Mac Cache Cleanup Script
#!/bin/bash
# Function to get list of installed applications
get_installed_apps() {
find /Applications -maxdepth 1 -name "*.app" -print0 | xargs -0 -n1 basename | sed 's/\.app$//' | tr '[:upper:]' '[:lower:]'
find ~/Applications -maxdepth 1 -name "*.app" -print0 2>/dev/null | xargs -0 -n1 basename | sed 's/\.app$//' | tr '[:upper:]' '[:lower:]'
}
# Get list of installed apps
INSTALLED_APPS=($(get_installed_apps))
# Directories to clean
CACHE_DIRS=(
"$HOME/Library/Caches"
"$HOME/Library/Application Support"
"/Library/Caches"
)
# Function to check if a string contains system-related keywords
is_system_related() {
local name=$1
[[ "$name" =~ (com\.apple|apple|system|framework|webkit|coreservice) ]]
}
# Function to check if an app is installed
is_app_installed() {
local app_name=$(echo "$1" | tr '[:upper:]' '[:lower:]')
[[ " ${INSTALLED_APPS[*]} " =~ " ${app_name} " ]]
}
# Main cleanup function
cleanup_caches() {
for dir in "${CACHE_DIRS[@]}"; do
if [ -d "$dir" ]; then
echo "Cleaning $dir"
for item in "$dir"/*; do
if [ -d "$item" ]; then
item_name=$(basename "$item" | tr '[:upper:]' '[:lower:]')
if ! is_system_related "$item_name" && ! is_app_installed "$item_name"; then
echo "Removing cache for uninstalled app: $item"
sudo rm -rf "$item"
fi
fi
done
else
echo "Directory $dir does not exist, skipping..."
fi
done
}
# Run the cleanup
echo "Starting cache cleanup..."
cleanup_caches
echo "Cache cleanup completed."
# Optionally, clear system caches
echo "Do you want to clear system caches as well? (y/n)"
read answer
if [[ $answer =~ ^[Yy]$ ]]; then
echo "Clearing system caches..."
sudo purge
echo "System caches cleared."
fi
echo "Cleanup process finished."
@ronnycoding
Copy link
Author

Dynamic Mac Cache Cleanup Script

This bash script provides a safe and efficient way to clean up cache files on macOS systems. It dynamically identifies installed applications and removes cache folders for uninstalled applications while preserving system-related caches.

Features

  • Dynamically lists installed applications from both /Applications and ~/Applications directories
  • Cleans cache folders in ~/Library/Caches, ~/Library/Application Support, and /Library/Caches
  • Preserves caches for currently installed applications and system-related caches
  • Optionally clears system caches using the purge command
  • Provides verbose output for removed cache folders

Usage

  1. Save the script to a file, e.g., dynamic_cleanup_cache.sh
  2. Make it executable: chmod +x dynamic_cleanup_cache.sh
  3. Run with sudo privileges: sudo ./dynamic_cleanup_cache.sh

Caution

  • Always back up important data before running cleanup scripts
  • Some applications may recreate their cache folders upon next launch
  • Use with caution and review the script before running

Requirements

  • macOS operating system
  • Bash shell
  • Sudo privileges for full functionality

How it works

  1. The script first generates a list of installed applications
  2. It then iterates through specified cache directories
  3. For each folder in these directories, it checks if:
    • The folder is not system-related (doesn't contain keywords like 'apple', 'system', etc.)
    • The folder doesn't correspond to a currently installed application
  4. If both conditions are met, the folder is removed
  5. Optionally, it can clear system caches using the purge command

Customization

You can modify the CACHE_DIRS array in the script to include or exclude specific directories for cleanup.

Disclaimer

This script is provided as-is, without any warranty. Use at your own risk. The author is not responsible for any data loss or system issues that may occur from using this script.

Contributing

Feel free to fork this script and submit pull requests for any improvements or bug fixes.

License

This script is released under the MIT License. See the LICENSE file for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment