-
Notifications
You must be signed in to change notification settings - Fork 5
/
cleanup.sh
executable file
·98 lines (75 loc) · 2.42 KB
/
cleanup.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
#!/usr/bin/env bash
# set -euo pipefail
# Imprort global functionality
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source "$script_dir/imports.sh"
pwd="$(pwd)"
cleanup() {
code=$?
# Go back to original directory
if cd "$pwd"; then
log "Success!"
else
log_error "Exit with status code $code"
exit $code
fi
}
trap "cleanup" INT TERM HUP EXIT
log "Clean up unused gem versions"
gem cleanup
log "Erase Caches Folder..."
sudo rm -rf "${HOME}/Library/Caches"
log "Remove Homebrew cache"
brew cleanup --prune=all
log "Remove All Unavailable Xcode Simulators"
xcrun simctl delete unavailable
log "Reset all Xcode Simulators"
killall "Simulator"
killall "iOS Simulator"
xcrun simctl shutdown all
xcrun simctl erase all
log "Cleanup Xcode DerivedData folder"
rm -rf "${HOME}/Library/Developer/Xcode/DerivedData/*"
log "Cleanup Xcode device logs"
rm -rf ~/Library/Developer/Xcode/iOS\ Device\ Logs/
log "Cleanup Xcode archives"
rm -rf ~/Library/Developer/Xcode/Archives
log "Cleanup CocoaPods Cache"
rm -rf ~/Library/Caches/CocoaPods
log "Cleanup Carthage Cache"
# rm -rf ~/Library/Caches/org.carthage.CarthageKit
log "Empty Trash"
# The -P option overwrites the deleted files for extra security (but that takes long)
sudo rm -rf ${HOME}/.Trash/*
log "Erase Spotlight Index and Rebuild"
sudo mdutil -E /
log "Reload Core Audio"
sudo kill -9 `pgrep 'coreaudio[a-z]' | awk '{print $1}'`
log "Create huge file and delete it again to re-claim hidden space from the system."
file="$(mktemp -d)/DELETE_THIS_DUMMY_FILE_TO_FREE_UP_SPACE.txt"
mkfile 200G $file
rm -rf $file
##
## The following is highly experimental!!!
## It is used to automatically delete iOS device support files and keeps
## the two most recent versions. Those are one of the biggest space
## killers on a Mac.
##
# set -euo pipefail
# 1: find all versions (folders) currently installed
# 2: reverse sort folders - use file name major version as key
# folders=$(find ${HOME}/Library/Developer/Xcode/iOS\ DeviceSupport -type d -d 1 | sort -t. -nr)
# 1: filter latest 2 major versions
# 2: remove path components after the version number
# 3: trim trailing whitespaces
# keep=$(echo "${folders[*]}" \
# | awk -F. 'a[$1]++<1' \
# | cut -d'(' -f1 \
# | sed 's/ *$//g' \
# | head -2 \
# | paste -s -d '|' -)
# echo "${folders[*]}" | grep -vE $keep
# echo "-----"
# echo "${folders[*]}"
# echo "-----"
# echo "${keep}"