Category filter
Script to clear cache on macOS devices
Cached data from website visits enhances online interactions by preserving useful information on users’ devices. Its major function is reducing dependence on server connectivity by keeping text, graphics, and data on the user’s device. This stored data reduces reliance on servers and speeds up access. However, periodically clearing the cached data and log files on your Mac will help protect your data and free up additional storage space. Moreover, deleting the app cache can also help fix issues with certain apps and ensure the smooth operation of your devices. Run this bash script using Hexnode’s Execute Custom Script action to delete all the cached data stored on your macOS devices.
Scripting language – Bash
File extension – .sh
Clear all cache
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#!/bin/bash USER=`stat -f%Su /dev/console` #System Caches sudo mv /private/var/log/privoxy /private/var/privoxy > /dev/null 2>&1 sudo /bin/rm -rf /private/var/log/* > /dev/null 2>&1 sudo mv /private/var/privoxy /private/var/log/privoxy > /dev/null 2>&1 #System Caches sudo /bin/rm -rf /Users/$USER/Library/Logs/* > /dev/null 2>&1 & sudo /bin/rm -rf /Library/Logs/DiagnosticReports/*.* > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/tmp/com.apple.messages > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Caches/* > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/db/diagnostics/*/* > /dev/null 2>&1 & sudo /bin/rm -rf /Library/Logs/DiagnosticReports/ProxiedDevice-Bridge/*.ips > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/CrashReporter/* > /dev/null 2>&1 & sudo /bin/rm -rf /private/tmp/gzexe* > /dev/null 2>&1 #Safari Caches sudo /bin/rm -rf /Users/$USER/Library/Containers/com.apple.Safari/Data/Library/Caches/* > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/folders/ry/*/*/com.apple.Safari/com.apple.Safari/com.apple.metal/*/libraries.data > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/folders/ry/*/*/com.apple.Safari/com.apple.Safari/com.apple.metal/*/libraries.maps > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Containers/io.te0.WebView/Data/Library/Caches/WebKit > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Safari/History.db* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Safari/RecentlyClosedTabs.plist > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Safari/CloudHistoryRemoteConfiguration.plist > /dev/null 2>&1 #Chrome Caches ChromePath="/Applications/Google Chrome.app" if [[ -d $ChromePath ]]; then sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/GPUCache/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Storage/ext/*/def/GPUCache/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*-journal > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/databases/*-journal > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Visited\ Links > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Top\ Sites > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/History\ Provider\ Cache > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Current\ Tabs > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Network\ Action\ Predictor > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*.ldb > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*.log > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Extension\ State/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Session\ Storage/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Current\ Session > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Storage/ext/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*/Cache > /dev/null 2>&1 fi #Clean Download History sudo sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'delete from LSQuarantineEvent' > /dev/null 2>&1 #Clean Terminal History sudo /bin/rm -rf /Users/$USER/.bash_sessions/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/.bash_history > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/.zsh_sessions/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/.zsh_history > /dev/null 2>&1 #Applications Caches for x in $(ls ~/Library/Containers/) do echo "Cleaning ~/Library/Containers/$x/Data/Library/Caches/" rm -rf ~/Library/Containers/$x/Data/Library/Caches/* done echo done |
Initially, the script clears the system cache which might consist of logs, caches, and diagnostic reports from various locations within the system. It can also include user caches that contain temporary files and data created for a smooth user experience.
After clearing system cache, the script clears browsing cache for different browsers such as Google Chrome, Safari, Opera browser and Mozilla Firefox. The script checks if the respective app is present on the device or not, before proceeding to delete its cached data. If the app is present, the script will clear its cached data such as GPU cache, visited links, session storage, and more.
Finally, the terminal history and application caches will be cleared from the system.