Skip to content

Commit 55e6428

Browse files
committed
Add preview-first Xcode cleanup guard
0 parents  commit 55e6428

6 files changed

Lines changed: 347 additions & 0 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: test
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
test:
12+
runs-on: macos-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Verify preview-only cleanup guard
16+
run: ./tests/test.sh

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 KikuAI-Lab
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Xcode Hygiene
2+
3+
A small, dependency-free cleanup guard for Xcode development machines.
4+
5+
It reports Xcode storage without changing anything by default. Destructive
6+
actions require both `--apply` and an explicit category. The script never
7+
touches archives, provisioning profiles, app containers, recovery copies, or
8+
physical-device support.
9+
10+
## Why
11+
12+
Xcode can quietly accumulate DerivedData and several multi-gigabyte simulator
13+
devices. This tool was extracted after a real cleanup recovered about 22 GB
14+
while preserving the one iPhone simulator, one Watch simulator, current device
15+
support, signing profiles, and application data that were still useful.
16+
17+
## Preview
18+
19+
```sh
20+
./xcode-hygiene.sh
21+
```
22+
23+
Preview a specific cleanup:
24+
25+
```sh
26+
./xcode-hygiene.sh \
27+
--clean-derived-data \
28+
--delete-unavailable-simulators \
29+
--prune-simulators \
30+
--keep-simulator "iPhone 17" \
31+
--keep-simulator "Apple Watch Series 11 (46mm)"
32+
```
33+
34+
The second command still changes nothing. It prints the devices that would be
35+
deleted.
36+
37+
## Apply
38+
39+
Run the same command with `--apply` only after reviewing the preview:
40+
41+
```sh
42+
./xcode-hygiene.sh \
43+
--apply \
44+
--clean-derived-data \
45+
--delete-unavailable-simulators \
46+
--prune-simulators \
47+
--keep-simulator "iPhone 17" \
48+
--keep-simulator "Apple Watch Series 11 (46mm)"
49+
```
50+
51+
The script refuses to continue while `xcodebuild` or an XCTest runner is
52+
active. Simulator pruning also refuses to run unless every requested keep-name
53+
exists in the current device set.
54+
55+
## Deliberately not automated
56+
57+
- `Archives`: these may be the only reproducible signed release artifacts.
58+
- `Provisioning Profiles`: deleting them can break local-device installation.
59+
- `iOS DeviceSupport`: verify the physical device OS before deleting an older
60+
support directory.
61+
- Simulator app data you did not explicitly prune.
62+
- Project directories, backups, and app containers.
63+
64+
## Verify
65+
66+
```sh
67+
./tests/test.sh
68+
```
69+
70+
Requires macOS, Xcode command-line tools, Zsh, and the system Python 3 shipped
71+
with Xcode.
72+
73+
## License
74+
75+
MIT

‎tests/test.sh‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/zsh
2+
3+
set -euo pipefail
4+
5+
script_path="${0:A}"
6+
repo_root="${script_path:h:h}"
7+
guard="$repo_root/xcode-hygiene.sh"
8+
9+
[[ -x "$guard" ]] || {
10+
print -u2 "test: xcode-hygiene.sh is not executable"
11+
exit 1
12+
}
13+
14+
zsh -n "$guard"
15+
"$guard" --help | grep -Fq -- '--clean-derived-data'
16+
17+
if "$guard" --unknown >/dev/null 2>&1; then
18+
print -u2 "test: unknown option unexpectedly succeeded"
19+
exit 1
20+
fi
21+
22+
if "$guard" --apply >/dev/null 2>&1; then
23+
print -u2 "test: empty apply unexpectedly succeeded"
24+
exit 1
25+
fi
26+
27+
if "$guard" --apply --prune-simulators >/dev/null 2>&1; then
28+
print -u2 "test: simulator prune without a keep-name unexpectedly succeeded"
29+
exit 1
30+
fi
31+
32+
"$guard" --clean-derived-data >/dev/null
33+
34+
print "xcode-hygiene tests passed."

‎xcode-hygiene.sh‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#!/bin/zsh
2+
3+
set -euo pipefail
4+
5+
fail() {
6+
print -u2 -- "xcode-hygiene: $1"
7+
exit 1
8+
}
9+
10+
usage() {
11+
cat <<'EOF'
12+
Usage: ./xcode-hygiene.sh [options]
13+
14+
Preview is the default. Nothing is deleted unless --apply is present.
15+
16+
Options:
17+
--apply Perform the selected cleanup actions.
18+
--clean-derived-data Delete contents of Xcode DerivedData.
19+
--delete-unavailable-simulators Delete simulator devices unavailable to Xcode.
20+
--prune-simulators Delete available simulators except keep-names.
21+
--keep-simulator NAME Preserve every simulator with this exact name.
22+
Repeat for more than one name.
23+
-h, --help Show this help.
24+
EOF
25+
}
26+
27+
apply_changes=false
28+
clean_derived_data=false
29+
delete_unavailable=false
30+
prune_simulators=false
31+
typeset -a keep_simulator_names=()
32+
33+
while (( $# > 0 )); do
34+
case "$1" in
35+
--apply)
36+
apply_changes=true
37+
shift
38+
;;
39+
--clean-derived-data)
40+
clean_derived_data=true
41+
shift
42+
;;
43+
--delete-unavailable-simulators)
44+
delete_unavailable=true
45+
shift
46+
;;
47+
--prune-simulators)
48+
prune_simulators=true
49+
shift
50+
;;
51+
--keep-simulator)
52+
(( $# >= 2 )) || fail "--keep-simulator requires an exact device name"
53+
[[ -n "$2" ]] || fail "simulator keep-name cannot be empty"
54+
keep_simulator_names+=("$2")
55+
shift 2
56+
;;
57+
-h|--help)
58+
usage
59+
exit 0
60+
;;
61+
*)
62+
fail "unknown option: $1"
63+
;;
64+
esac
65+
done
66+
67+
if $prune_simulators && (( ${#keep_simulator_names[@]} == 0 )); then
68+
fail "--prune-simulators requires at least one --keep-simulator"
69+
fi
70+
71+
if $apply_changes \
72+
&& ! $clean_derived_data \
73+
&& ! $delete_unavailable \
74+
&& ! $prune_simulators; then
75+
fail "--apply requires at least one cleanup category"
76+
fi
77+
78+
current_user="$(/usr/bin/id -un)"
79+
home_record="$(/usr/bin/dscl . -read "/Users/$current_user" NFSHomeDirectory 2>/dev/null)" \
80+
|| fail "could not resolve the current user's home directory"
81+
user_home="${home_record#* }"
82+
83+
derived_data_root="$user_home/Library/Developer/Xcode/DerivedData"
84+
device_support_root="$user_home/Library/Developer/Xcode/iOS DeviceSupport"
85+
simulator_root="$user_home/Library/Developer/CoreSimulator"
86+
87+
[[ "$user_home" == /Users/* && -d "$user_home" ]] \
88+
|| fail "resolved an unsafe home directory"
89+
[[ "$derived_data_root" == "$user_home/Library/Developer/Xcode/DerivedData" ]] \
90+
|| fail "resolved an unsafe DerivedData path"
91+
[[ "$simulator_root" == "$user_home/Library/Developer/CoreSimulator" ]] \
92+
|| fail "resolved an unsafe simulator path"
93+
94+
size_or_missing() {
95+
local label="$1"
96+
local target="$2"
97+
if [[ -e "$target" ]]; then
98+
print -- "$label: $(/usr/bin/du -sh "$target" 2>/dev/null | /usr/bin/awk '{print $1}')"
99+
else
100+
print -- "$label: missing"
101+
fi
102+
}
103+
104+
print "Mode: $($apply_changes && print apply || print preview)"
105+
size_or_missing "DerivedData" "$derived_data_root"
106+
size_or_missing "iOS DeviceSupport" "$device_support_root"
107+
size_or_missing "CoreSimulator" "$simulator_root"
108+
print "Disk: $(/bin/df -h / | /usr/bin/tail -n 1 | /usr/bin/awk '{print $4 " available"}')"
109+
print
110+
/usr/bin/xcrun simctl list devices
111+
112+
temporary_root=""
113+
cleanup_temporary_root() {
114+
if [[ -n "$temporary_root" \
115+
&& -d "$temporary_root" \
116+
&& "${temporary_root:t}" == xcode-hygiene.* ]]; then
117+
/usr/bin/find "$temporary_root" -depth -delete
118+
fi
119+
}
120+
trap cleanup_temporary_root EXIT
121+
122+
typeset -a prune_candidates=()
123+
if $prune_simulators; then
124+
temporary_root="$(/usr/bin/mktemp -d "${TMPDIR:-/tmp}/xcode-hygiene.XXXXXX")"
125+
simulator_json="$temporary_root/simulators.json"
126+
candidate_file="$temporary_root/candidates.tsv"
127+
/usr/bin/xcrun simctl list devices available --json > "$simulator_json"
128+
129+
/usr/bin/python3 - "$simulator_json" "$candidate_file" "${keep_simulator_names[@]}" <<'PY'
130+
import json
131+
import sys
132+
133+
source, destination, *keep_names = sys.argv[1:]
134+
keep = set(keep_names)
135+
found = {name: False for name in keep}
136+
rows = []
137+
138+
with open(source, encoding="utf-8") as handle:
139+
devices_by_runtime = json.load(handle)["devices"]
140+
141+
for runtime, devices in devices_by_runtime.items():
142+
for device in devices:
143+
name = device["name"]
144+
if name in keep:
145+
found[name] = True
146+
else:
147+
rows.append((device["udid"], name, runtime))
148+
149+
missing = sorted(name for name, present in found.items() if not present)
150+
if missing:
151+
print("missing keep-name(s): " + ", ".join(missing), file=sys.stderr)
152+
sys.exit(2)
153+
154+
with open(destination, "w", encoding="utf-8") as handle:
155+
for row in rows:
156+
handle.write("\t".join(row) + "\n")
157+
PY
158+
159+
while IFS=$'\t' read -r simulator_id simulator_name simulator_runtime; do
160+
[[ -n "$simulator_id" ]] || continue
161+
prune_candidates+=("$simulator_id")
162+
print -- "Prune candidate: $simulator_name [$simulator_id] ($simulator_runtime)"
163+
done < "$candidate_file"
164+
fi
165+
166+
if ! $apply_changes; then
167+
print
168+
print "Preview complete. Re-run with --apply to perform the selected actions."
169+
exit 0
170+
fi
171+
172+
if /usr/bin/pgrep -x xcodebuild >/dev/null 2>&1 \
173+
|| /usr/bin/pgrep -f 'xctest|UITests-Runner|XCTRunner' >/dev/null 2>&1; then
174+
fail "an Xcode build or test runner is active"
175+
fi
176+
177+
if $prune_simulators || $delete_unavailable; then
178+
/usr/bin/xcrun simctl shutdown all
179+
fi
180+
181+
if $prune_simulators; then
182+
for simulator_id in "${prune_candidates[@]}"; do
183+
/usr/bin/xcrun simctl delete "$simulator_id"
184+
done
185+
fi
186+
187+
if $delete_unavailable; then
188+
/usr/bin/xcrun simctl delete unavailable
189+
fi
190+
191+
if $clean_derived_data && [[ -d "$derived_data_root" ]]; then
192+
/usr/bin/find "$derived_data_root" -mindepth 1 -depth -delete
193+
fi
194+
195+
print
196+
print "Cleanup complete."
197+
size_or_missing "DerivedData" "$derived_data_root"
198+
size_or_missing "iOS DeviceSupport" "$device_support_root"
199+
size_or_missing "CoreSimulator" "$simulator_root"
200+
print "Disk: $(/bin/df -h / | /usr/bin/tail -n 1 | /usr/bin/awk '{print $4 " available"}')"

0 commit comments

Comments
 (0)