-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear_system_cache.sh
executable file
·160 lines (137 loc) · 5.43 KB
/
clear_system_cache.sh
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
set -e
echo "====================================================="
echo " System Cache Cleaning Utility"
echo "====================================================="
echo
echo "WARNING: This script will clear various cache files."
echo "While this is generally safe, it may affect performance temporarily"
echo "as applications and the system rebuild their caches."
echo
# Function to check disk space and report difference
check_space() {
df -h / | grep -v Filesystem
}
# Store initial space
echo "Current disk space usage:"
check_space
echo
# Helper function to safely clear cache directories
clear_cache_dir() {
local dir=$1
local description=$2
local use_sudo=$3
echo "Checking $description..."
if [ ! -d "$dir" ]; then
echo "$description directory not found. Skipping."
return
fi
read -p "Clear $description? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Count files before
local count_before=0
if [ "$use_sudo" = true ]; then
count_before=$(sudo find "$dir" -type f | wc -l)
else
count_before=$(find "$dir" -type f | wc -l)
fi
echo "Found $count_before files in $description."
# Delete contents but not the directory itself
if [ "$use_sudo" = true ]; then
sudo find "$dir" -mindepth 1 -delete
else
find "$dir" -mindepth 1 -delete
fi
echo "$description cleared successfully."
check_space
else
echo "Skipping $description cleanup."
fi
echo
}
# Browser caches
echo "====================================================="
echo "Browser Caches"
echo "====================================================="
clear_cache_dir "$HOME/Library/Caches/com.apple.Safari" "Safari cache" false
clear_cache_dir "$HOME/Library/Safari/LocalStorage" "Safari local storage" false
clear_cache_dir "$HOME/Library/Caches/Google/Chrome" "Chrome cache" false
clear_cache_dir "$HOME/Library/Caches/com.microsoft.Edge" "Edge cache" false
clear_cache_dir "$HOME/Library/Caches/org.mozilla.firefox" "Firefox cache" false
# App development caches
echo "====================================================="
echo "Development Caches"
echo "====================================================="
clear_cache_dir "$HOME/Library/Developer/Xcode/DerivedData" "Xcode derived data" false
clear_cache_dir "$HOME/Library/Developer/CoreSimulator/Caches" "iOS simulator caches" false
clear_cache_dir "$HOME/Library/Caches/com.apple.dt.Xcode" "Xcode caches" false
# System caches (with caution)
echo "====================================================="
echo "System Caches"
echo "====================================================="
echo "These caches are generally safe to clear but will rebuild over time."
# Font caches
clear_cache_dir "/Library/Caches/com.apple.fontservice" "Font caches" true
clear_cache_dir "$HOME/Library/Caches/com.apple.FontRegistry" "Font registry cache" false
# App store and software update caches
clear_cache_dir "/Library/Caches/com.apple.appstore" "App Store cache" true
clear_cache_dir "$HOME/Library/Caches/com.apple.appstore" "User App Store cache" false
# System app caches (select ones that are safe to clear)
echo "====================================================="
echo "Application Caches"
echo "====================================================="
echo "Clearing these can free up space but may cause temporary slowdowns."
read -p "Would you like to clear application caches? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Only target known safe caches, not ALL caches
echo "Clearing select application caches..."
# System apps
sudo find /Library/Caches -name "*.db" -delete 2>/dev/null || true
# User apps (with exclusions for critical ones)
find "$HOME/Library/Caches" -mindepth 1 -maxdepth 1 | grep -v "com.apple.dock" | grep -v "com.apple.finder" | while read cache_dir; do
if [ -d "$cache_dir" ]; then
app_name=$(basename "$cache_dir")
echo "Clearing $app_name cache"
rm -rf "$cache_dir"/* 2>/dev/null || true
fi
done
echo "Application caches cleared."
check_space
else
echo "Skipping application caches."
fi
# Reset App Store download history cache
echo "====================================================="
echo "App Store Download History"
echo "====================================================="
read -p "Reset App Store download history cache? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
defaults delete com.apple.appstore DownloadHistoryItem 2>/dev/null || true
killall "App Store" 2>/dev/null || true
echo "App Store download history reset."
else
echo "Skipping App Store history reset."
fi
# Clean up system logs
echo "====================================================="
echo "System Logs"
echo "====================================================="
read -p "Clear system logs? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo rm -rf /private/var/log/asl/*.asl 2>/dev/null || true
sudo rm -rf /private/var/log/DiagnosticMessages/* 2>/dev/null || true
echo "System logs cleared."
check_space
else
echo "Skipping system logs cleanup."
fi
# Final report
echo "====================================================="
echo " Cache cleanup complete!"
echo " Final disk space usage:"
check_space
echo "====================================================="