Skip to content

Instantly share code, notes, and snippets.

@ningsuhen
Last active October 30, 2015 10:40
Show Gist options
  • Save ningsuhen/504d905722114c1bb1fe to your computer and use it in GitHub Desktop.
Save ningsuhen/504d905722114c1bb1fe to your computer and use it in GitHub Desktop.
Simple Shell Script for cleaning up XCode files - Credit - https://github.com/Seitk/Xcode-Cleanup-Script

Xcode-Cleanup-Script

My tiny Macbook Air was hungry for disk space and there were more than 18GB of unused files for xcode caching and application supports. So I wrote a shell script to cleanup those files. For files to be deleted and their usage, check this blog (http://blog.favo.org/post/31649090293/xcode-5-places-to-save-some-disk-space).

Files to be removed
~/Library/Developer/Xcode/Archives/*
~/Library/Developer/Xcode/DerivedData/*
~/Library/Developer/Xcode/iOS DeviceSupport/*
~/Library/Application Support/iPhone Simulator/?/Applications
~/Library/Application Support/iPhone Simulator/?/tmp/ghostlyIcons.*
~/Library/Application Support/iPhone Simulator/?/tmp/gridImages.*
~/Library/Application Support/iPhone Simulator/?/tmp/iconImages.*
~/Library/Application Support/iPhone Simulator/?/tmp/iconLabels_gray.*

? : Version of iOS Simulator

Usage

./xcode_cleanup.sh

#!/bin/sh
echo "========== Cleanup start =========="
# define paths
archivesPath="/Library/Developer/Xcode/Archives"
derivedDataPath="/Library/Developer/Xcode/DerivedData"
oldDeviceInfoPath="/Library/Developer/Xcode/iOS DeviceSupport"
simulatorApplicationRootPath="/Library/Application Support/iPhone Simulator/"
USER_HOME=$(eval echo ~${SUDO_USER})
paths=( "$archivesPath" "$derivedDataPath" "$oldDeviceInfoPath" )
msgs=( "Archives" "DerivedData" "Old device information" )
pathsLength=${#paths[@]}
# Clean up developer folder
# loop through predefined paths
for (( i=0; i<${pathsLength}; i++ ));
do
CMD="sudo rm -rf "$USER_HOME${paths[$i]}"/*"
$CMD
echo ${msgs[$i]}" cleared"
done
# Clean up iOS Simulator
ignoreFolders=("Applications" "Containers" "Library" "Root" "User" "tmpspace")
versionMarker="."
rootPathIdx=$((${#USER_HOME} + ${#simulatorApplicationRootPath}))
for folderPath in "$USER_HOME$simulatorApplicationRootPath"*
do
length=${#folderPath}
folderName=${folderPath:$rootPathIdx:$((length - rootPathIdx))}
# Find folder in ~/Library/Application Support/iPhone Simulator/ with "." (assuming it is simulator version) and not in ignored list
if [[ "${ignoreFolders[*]}" != *$folderName* && $folderName == *$versionMarker* ]]; then
# Check if Applications folder exists
if [ -d "$folderPath/Applications" ]; then
echo 'iOS Simulator version '$folderName' with applications installed, now cleared'
tmpFolderPath="${folderPath// /*}"
CMD="sudo rm -rf "$tmpFolderPath"/Applications"
$CMD
fi
# Check if tmp folder exists
if [ -d "$folderPath/tmp" ]; then
# Check tmp files in folder with special prefixs (say ghostlyIcons.xqwj3qwb2)
tmpFolderPath="${folderPath// /*}"
hasTmpFiles=false
for ext in "ghostlyIcons" "gridImages" "iconImages" "iconLabels_gray"
do
fileCheckPath="$tmpFolderPath/tmp/$ext.*"
if (ls $fileCheckPath > /dev/null 2>&1)
then
CMD="sudo rm -rf "$tmpFolderPath"/tmp/$ext.*"
$CMD
hasTmpFiles=true
fi
done
if ($hasTmpFiles); then
echo 'iOS Simulator version '$folderName' tmp files cleared'
fi
fi
fi
done
echo "========== Cleanup ended =========="
echo "Suggest you to restart your xcode"
echo "========== Have a nice day =========="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment