-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanmac.sh
More file actions
executable file
·318 lines (257 loc) · 8.77 KB
/
cleanmac.sh
File metadata and controls
executable file
·318 lines (257 loc) · 8.77 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
################################################################################
# CleanMac — Ultimate macOS Cleanup CLI Tool
# Author: Shaswat Raj (Shade Solutions)
# GitHub: https://github.com/shade-solutions/cleanmac
# License: MIT
# Platform: macOS (Intel, M1, M2, M3)
################################################################################
set -e
# Color codes for better terminal output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored messages
print_info() {
echo -e "${BLUE}➡️ $1${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
# Function to get disk usage
get_disk_usage() {
df -h / | awk 'NR==2 {print $3 " used / " $4 " free (" $5 " used)"}'
}
# Function to calculate directory size
get_dir_size() {
if [ -d "$1" ]; then
du -sh "$1" 2>/dev/null | awk '{print $1}' || echo "0B"
else
echo "0B"
fi
}
# Header
print_header() {
echo ""
echo "🧹 CleanMac — macOS Developer System Cleaner"
echo "-----------------------------------------------"
echo "📊 Disk before cleanup: $(get_disk_usage)"
echo ""
}
# Clean project folders (node_modules, .next, dist, build, etc.)
clean_project_folders() {
print_info "Cleaning project folders (node_modules, .next, dist, build, .expo, .vercel, .nuxt)..."
local dirs=("node_modules" ".next" ".nuxt" ".expo" ".vercel" "dist" "build" ".turbo")
local total_cleaned=0
for dir in "${dirs[@]}"; do
while IFS= read -r -d '' folder; do
size=$(du -sk "$folder" 2>/dev/null | awk '{print $1}')
rm -rf "$folder" 2>/dev/null && total_cleaned=$((total_cleaned + size))
done < <(find ~/Desktop ~/Documents ~/Projects ~ -maxdepth 5 -type d -name "$dir" -print0 2>/dev/null)
done
print_success "Done — cleaned project folders"
}
# Clean package manager caches
clean_package_caches() {
print_info "Cleaning package manager caches (npm, yarn, pnpm)..."
# npm cache
if command -v npm &> /dev/null; then
npm cache clean --force 2>/dev/null || true
fi
# yarn cache
if command -v yarn &> /dev/null; then
yarn cache clean 2>/dev/null || true
fi
# pnpm cache
if command -v pnpm &> /dev/null; then
pnpm store prune 2>/dev/null || true
fi
print_success "Done — cleaned package manager caches"
}
# Clean Homebrew cache
clean_homebrew() {
print_info "Cleaning Homebrew cache..."
if command -v brew &> /dev/null; then
brew cleanup -s 2>/dev/null || true
rm -rf "$(brew --cache)" 2>/dev/null || true
else
print_warning "Homebrew not found, skipping"
fi
print_success "Done — cleaned Homebrew"
}
# Clean Xcode DerivedData and device support
clean_xcode() {
print_info "Cleaning Xcode DerivedData and iOS DeviceSupport..."
rm -rf ~/Library/Developer/Xcode/DerivedData/* 2>/dev/null || true
rm -rf ~/Library/Developer/Xcode/iOS\ DeviceSupport/* 2>/dev/null || true
rm -rf ~/Library/Developer/Xcode/watchOS\ DeviceSupport/* 2>/dev/null || true
rm -rf ~/Library/Developer/Xcode/Archives/* 2>/dev/null || true
print_success "Done — cleaned Xcode"
}
# Clean CocoaPods cache
clean_cocoapods() {
print_info "Cleaning CocoaPods cache..."
rm -rf ~/Library/Caches/CocoaPods 2>/dev/null || true
if command -v pod &> /dev/null; then
pod cache clean --all 2>/dev/null || true
fi
print_success "Done — cleaned CocoaPods"
}
# Clean app caches (VSCode, Chrome, Discord, Slack)
clean_app_caches() {
print_info "Cleaning app caches (VSCode, Chrome, Discord, Slack)..."
# VSCode
rm -rf ~/Library/Caches/com.microsoft.VSCode 2>/dev/null || true
rm -rf ~/.vscode/extensions/*/node_modules 2>/dev/null || true
# Chrome
rm -rf ~/Library/Caches/Google/Chrome 2>/dev/null || true
# Discord
rm -rf ~/Library/Caches/com.hnc.Discord 2>/dev/null || true
rm -rf ~/Library/Application\ Support/Discord/Cache 2>/dev/null || true
rm -rf ~/Library/Application\ Support/Discord/Code\ Cache 2>/dev/null || true
# Slack
rm -rf ~/Library/Caches/com.tinyspeck.slackmacgap 2>/dev/null || true
rm -rf ~/Library/Application\ Support/Slack/Cache 2>/dev/null || true
rm -rf ~/Library/Application\ Support/Slack/Code\ Cache 2>/dev/null || true
print_success "Done — cleaned app caches"
}
# Clean Docker
clean_docker() {
print_info "Cleaning Docker (images, containers, volumes)..."
if command -v docker &> /dev/null; then
docker system prune -a -f --volumes 2>/dev/null || true
else
print_warning "Docker not found, skipping"
fi
print_success "Done — cleaned Docker"
}
# Clean user and system caches and logs
clean_system_caches() {
print_info "Cleaning user and system caches and logs..."
# User caches
rm -rf ~/Library/Caches/* 2>/dev/null || true
# User logs
rm -rf ~/Library/Logs/* 2>/dev/null || true
# System logs (requires sudo)
if [ "$EUID" -eq 0 ]; then
rm -rf /private/var/log/* 2>/dev/null || true
rm -rf /Library/Logs/* 2>/dev/null || true
else
print_warning "Skipping system logs (requires sudo)"
fi
print_success "Done — cleaned caches and logs"
}
# Clean trash, downloads, and temp files
clean_trash_downloads() {
print_info "Cleaning Trash, Downloads, and temporary files..."
# Empty Trash
rm -rf ~/.Trash/* 2>/dev/null || true
# Clean Downloads (optional - be careful!)
read -p "⚠️ Do you want to delete ALL files in Downloads? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf ~/Downloads/* 2>/dev/null || true
print_success "Downloads folder cleaned"
else
print_info "Skipping Downloads folder"
fi
# Temp files
rm -rf /tmp/* 2>/dev/null || true
rm -rf ~/Library/Application\ Support/CrashReporter/* 2>/dev/null || true
print_success "Done — cleaned trash and temp files"
}
# Find and delete large cache files (>1GB)
clean_large_files() {
print_info "Finding and deleting cache files larger than 1GB..."
find ~/Library/Caches -type f -size +1G -delete 2>/dev/null || true
print_success "Done — cleaned large cache files"
}
# Clean desktop screenshots
clean_screenshots() {
print_info "Cleaning Desktop screenshots..."
find ~/Desktop -name "Screen Shot*.png" -delete 2>/dev/null || true
find ~/Desktop -name "Screenshot*.png" -delete 2>/dev/null || true
print_success "Done — cleaned screenshots"
}
# Run all cleanup tasks
run_all_cleanup() {
clean_project_folders
clean_package_caches
clean_homebrew
clean_xcode
clean_cocoapods
clean_app_caches
clean_docker
clean_system_caches
clean_trash_downloads
clean_large_files
clean_screenshots
}
# Interactive menu
show_menu() {
echo ""
echo "Choose what to clean:"
echo ""
echo "1) Project folders (node_modules, .next, dist, build)"
echo "2) Package manager caches (npm, yarn, pnpm)"
echo "3) Homebrew cache"
echo "4) Xcode DerivedData and device support"
echo "5) CocoaPods cache"
echo "6) App caches (VSCode, Chrome, Discord, Slack)"
echo "7) Docker cleanup"
echo "8) User/system cache and logs"
echo "9) Trash, Downloads, temp"
echo "10) Large cache files"
echo "11) Desktop screenshots"
echo "12) Run ALL"
echo "0) Exit"
echo ""
}
# Main function
main() {
# Check if running on macOS
if [[ "$OSTYPE" != "darwin"* ]]; then
print_error "This script is designed for macOS only"
exit 1
fi
print_header
while true; do
show_menu
read -p "Enter your choice [0-12]: " choice
case $choice in
1) clean_project_folders ;;
2) clean_package_caches ;;
3) clean_homebrew ;;
4) clean_xcode ;;
5) clean_cocoapods ;;
6) clean_app_caches ;;
7) clean_docker ;;
8) clean_system_caches ;;
9) clean_trash_downloads ;;
10) clean_large_files ;;
11) clean_screenshots ;;
12) run_all_cleanup ;;
0)
echo ""
echo "🚀 Cleanup complete!"
echo "📊 Disk after cleanup: $(get_disk_usage)"
echo "✨ Your Mac is now lighter and faster!"
echo ""
exit 0
;;
*)
print_error "Invalid choice. Please try again."
;;
esac
done
}
# Run the script
main