Skip to content

Instantly share code, notes, and snippets.

@austinsonger
Last active October 21, 2025 03:15
Show Gist options
  • Select an option

  • Save austinsonger/5269c806d464cb789e917eea65a84d04 to your computer and use it in GitHub Desktop.

Select an option

Save austinsonger/5269c806d464cb789e917eea65a84d04 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
if [ "$EUID" -ne 0 ]; then
script_path=$([[ "$0" = /* ]] && echo "$0" || echo "$PWD/${0#./}")
sudo "$script_path" || (
echo 'Administrator privileges are required.'
exit 1
)
exit 0
fi
echo "====================================="
echo " macOS Privacy & Cleanup Utility"
echo "====================================="
# ----------------------------------------------------------
# Disable personalized advertisements and identifier tracking
echo '--- Disable personalized advertisements and identifier tracking'
defaults write com.apple.AdLib allowIdentifierForAdvertising -bool false
defaults write com.apple.AdLib allowApplePersonalizedAdvertising -bool false
defaults write com.apple.AdLib forceLimitAdTracking -bool true
# ----------------------------------------------------------
# ----------------------------------------------------------
# Clear shell histories
echo '--- Clear zsh and bash histories'
rm -f ~/.zsh_history ~/.bash_history
# ----------------------------------------------------------
# ----------------------------------------------------------
# Clear diagnostic and system logs
echo '--- Clearing system diagnostic and log files...'
sudo rm -rfv /private/var/db/diagnostics/* /private/var/db/uuidtext/* /private/var/log/asl/* \
/private/var/log/install.log /private/var/log/* /Library/Logs/* "$HOME/Library/Logs"/*
sudo rm -rfv /private/var/db/receipts/* /Library/Receipts/InstallHistory.plist \
/private/var/log/daily.out /private/var/log/weekly.out /private/var/log/monthly.out
sudo rm -rfv /private/var/audit/*
# ----------------------------------------------------------
# ----------------------------------------------------------
# Clear Mail logs
echo '--- Clear Mail app logs'
rm -rfv "$HOME/Library/Containers/com.apple.mail/Data/Library/Logs/Mail/"*
# ----------------------------------------------------------
# ----------------------------------------------------------
# Firefox cleanup
echo '--- Clear Firefox data'
rm -rfv ~/Library/Caches/Mozilla/
rm -fv ~/Library/Application\ Support/Firefox/Profiles/*/{netpredictions.sqlite,formhistory.sqlite,formhistory.dat,content-prefs.sqlite,sessionstore*,signons*,logins.json,webappsstore.sqlite,cookies*,bookmarkbackups/*.json*,Crash\ Reports/,minidumps/*.dmp,storage/default/http*}
# ----------------------------------------------------------
# ----------------------------------------------------------
# Safari cleanup
echo '--- Clear Safari caches, cookies, and history'
rm -f ~/Library/Caches/com.apple.Safari/Cache.db
rm -f ~/Library/Safari/{WebpageIcons.db,History.db*,Downloads.plist,TopSites.plist,LastSession.plist,PerSiteZoomPreferences.plist,UserNotificationPreferences.plist,PerSitePreferences.db}
rm -rfv ~/Library/Caches/com.apple.Safari/Webpage\ Previews
rm -rfv ~/Library/Caches/Metadata/Safari/History
defaults write ~/Library/Preferences/com.apple.Safari RecentSearchStrings '( )'
rm -f ~/Library/Cookies/Cookies.binarycookies ~/Library/Cookies/Cookies.plist
# ----------------------------------------------------------
# ----------------------------------------------------------
# CUPS printer job cache
echo '--- Clear CUPS printer job cache'
sudo rm -rfv /var/spool/cups/{c0*,tmp/*,cache/job.cache*}
# ----------------------------------------------------------
# ----------------------------------------------------------
# Empty all trashes
echo '--- Emptying all trash bins...'
sudo rm -rfv /Volumes/*/.Trashes/* &>/dev/null
sudo rm -rfv ~/.Trash/* &>/dev/null
# ----------------------------------------------------------
# ----------------------------------------------------------
# Clear system cache
echo '--- Clearing system cache'
sudo rm -rfv /Library/Caches/* /System/Library/Caches/* ~/Library/Caches/* &>/dev/null
# ----------------------------------------------------------
# ----------------------------------------------------------
# Clear Xcode build data
echo '--- Clearing Xcode derived data and archives'
rm -rfv ~/Library/Developer/Xcode/{DerivedData,Archives,iOS\ Device\ Logs}/* &>/dev/null
# ----------------------------------------------------------
# ----------------------------------------------------------
# Flush DNS cache
echo '--- Flushing DNS cache'
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
# ----------------------------------------------------------
# ----------------------------------------------------------
# Clear inactive memory
echo '--- Clearing inactive memory'
sudo purge
# ----------------------------------------------------------
# ----------------------------------------------------------
# Add extra macOS cleanup (from earlier utility)
echo '--- Cleaning user/system caches and logs (additional cleanup)'
sudo rm -rf /Library/Caches/*
sudo rm -rf "$HOME/Library/Caches"/*
sudo rm -rf /private/var/log/*
sudo rm -rf "$HOME/Library/Logs"/*
sudo rm -rf "$HOME/Library/Application Support/MobileSync/Backup"
sudo tmutil thinlocalsnapshots / 999999999999 2>/dev/null || true
# ----------------------------------------------------------
# ----------------------------------------------------------
# Prompt for optional large file listing
read -p "Would you like to list files larger than 500MB in your home folder? (y/n): " large
if [[ "$large" == "y" ]]; then
echo "Listing large files..."
find "$HOME" -type f -size +500M -print 2>/dev/null
fi
# ----------------------------------------------------------
echo
echo '✅ Your privacy and system cleanup are complete!'
echo 'Press any key to exit.'
read -n 1 -s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment