Skip to content

Instantly share code, notes, and snippets.

@arthurdapaz
Created July 25, 2020 00:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save arthurdapaz/e2a4fc83fa561ec52af83a5b08d0497d to your computer and use it in GitHub Desktop.
Save arthurdapaz/e2a4fc83fa561ec52af83a5b08d0497d to your computer and use it in GitHub Desktop.
Safely Reset Xcode from 9.3 to 11.6. Clean, clear module cache, Derived Data and Xcode Caches.
#!/bin/zsh
############
### zsh script to clear xcode caches and project
###
### Works from Xcode 9.3 to Xcode 11.6
###
### last date I tested: (7/24/2020)
###
### Move this script to /usr/bin/local so this will be the default usage:
### cd myXcodeWorkspace/
### xclean
###
############
# clean up function
function cleanXcode() {
setopt nomatch
pkill -int com.apple.CoreSimulator.CoreSimulatorService
killall Xcode
xcrun -k
xcodebuild -alltargets clean
rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang/ModuleCache"
rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang.$(whoami)/ModuleCache"
rm -rf ~/Library/Developer/Xcode/DerivedData/*
rm -rf ~/Library/Caches/com.apple.dt.Xcode/*
xed "$TARGET" .
}
# ensures that you are inside a Xcode folder
function ensureFolder() {
local workspace project return variable=$1
workspace=(*.xcworkspace)
project=(*.xcodeproj)
if [ -e "${workspace}" ] || [ -e "${project}" ]; then
return=true
else
return=false
fi
eval $variable="'$return'"
}
# script main entry point
unsetopt nomatch
ensureFolder contains_xcode_project
if $contains_xcode_project; then
echo "✓ Cleaning Xcode..."
cleanXcode
else
echo "⨯ Xcode workspace or project not found"
fi
@saxrub
Copy link

saxrub commented Aug 1, 2020

Hi Arthur
When I run xclean.sh, i get this message : xCode workspace or projet not found
Can you help me ?
Pascal

@sachinroy19
Copy link

Great! Worked for me

@arthurdapaz
Copy link
Author

Hi Arthur
When I run xclean.sh, i get this message : xCode workspace or projet not found
Can you help me ?
Pascal

The script is pretty self explanatory. I made it to work with zsh (shell), if you want to use safely with other shell, you need to change the firstline of the script. But if you have zsh installed it should run just fine.

After that, just make sure that you are indeed inside a folder which contains a workspace or a xcodeproject (or both) and run the command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment