Skip to content

Instantly share code, notes, and snippets.

@squeaky-nose
Last active January 4, 2026 22:55
Show Gist options
  • Select an option

  • Save squeaky-nose/9e07ec1813f38641e730ec3e2da0746d to your computer and use it in GitHub Desktop.

Select an option

Save squeaky-nose/9e07ec1813f38641e730ec3e2da0746d to your computer and use it in GitHub Desktop.
Clean swift artifacts
#!/bin/bash
# Created with ChatGPT... https://chatgpt.com/share/691516e4-0374-8010-b82a-93d0ad6277ec
cleanSwiftPM=false
cleanDerivedData=false
autoYes=false
usage() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " --swiftpm Delete SwiftPM directories"
echo " --deriveddata Delete Xcode DerivedData"
echo " --all Delete ALL known items (SwiftPM + DerivedData)"
echo " -y, --yes Skip confirmation prompt"
echo " -h, --help Show this help"
exit 1
}
if [[ $# -eq 0 ]]; then usage; fi
while [[ $# -gt 0 ]]; do
case "$1" in
--swiftpm) cleanSwiftPM=true; shift ;;
--deriveddata) cleanDerivedData=true; shift ;;
--all) cleanSwiftPM=true; cleanDerivedData=true; shift ;;
-y|--yes) autoYes=true; shift ;;
-h|--help) usage ;;
*) echo "Unknown option: $1"; usage ;;
esac
done
declare -a TARGETS=()
if $cleanSwiftPM; then
TARGETS+=("$HOME/Library/org.swift.swiftpm")
TARGETS+=("$HOME/Library/Caches/org.swift.swiftpm")
fi
if $cleanDerivedData; then
TARGETS+=("$HOME/Library/Developer/Xcode/DerivedData")
fi
if [[ ${#TARGETS[@]} -eq 0 ]]; then
echo "Nothing selected to delete."
exit 1
fi
echo "The following directories will be moved to a temp folder and deleted:"
for p in "${TARGETS[@]}"; do
echo " - $p"
done
echo
if ! $autoYes; then
read -r -p "Are you sure? (y/N): " confirm
confirm=$(echo "$confirm" | tr '[:upper:]' '[:lower:]')
if [[ "$confirm" != "y" && "$confirm" != "yes" ]]; then
echo "Cancelled."
exit 0
fi
else
echo "--yes specified, skipping confirmation."
fi
TMP_DIR="$(mktemp -d "$HOME/.dev-clean-XXXXXX")"
echo "Using temp directory: $TMP_DIR"
echo
move_preserving_structure() {
local src="$1"
if [[ ! -e "$src" ]]; then
echo "Skipping (not found): $src"
return 0
fi
local rel="${src#"$HOME"/}"
local dest="$TMP_DIR/$rel"
local dest_parent
dest_parent="$(dirname "$dest")"
mkdir -p "$dest_parent"
echo "Moving $src -> $dest"
mv "$src" "$dest"
}
for p in "${TARGETS[@]}"; do
move_preserving_structure "$p"
done
echo
echo "Deleting temp directory..."
rm -rf "$TMP_DIR"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment