How to Create an Xcode Reset Script

    If you have a large project with multiple types of dependencies, switching Git branches can be a headache. Xcode will fail to build and the error messages are often cryptic. For simple projects, a basic project clean will resolve most issues. You can do this through the Xcode menu Product -> Clean Build Folder or the keyboard shortcut Shift + Cmd + K. However, if you have a project that uses multiple Swift Packages and CocoaPods, figuring out which caches to clear can be difficult. One option is to create a shell script that can run multiple clean commands all at once.

    First, you’ll need to list all the commands you need. For this example, we’ll clear the Xcode build files, clear the Git cache, install the CocoaPods dependencies, reset the Swift Packages caches, and finally, open Xcode once it’s done.

    Open a plain text editor and create new file and name it something like xcodereset.sh. Add this line at the top, to indicate that it’s an executable shell script:

    #!/usr/bin/env bash

    This assumes you are using bash as your shell.
    Next, let’s print out a simple statement to make sure it can be executed. Add this on the next line:

    echo 'Hello bash!'

    Open your terminal and cd into the directory where you saved your file. We need to make the file executable, so run this command:

    chmod u+x xcodereset.sh

    If you then run ls -l you should see the permissions as:

    -rwxr--r--

    Now you can run:

    ./xcodereset.sh

    You should see “Hello bash!” printed out in the console.
    Now that we can execute it, let’s add our real commands.
    Replace:

    echo 'Hello bash!'

    with:

    set -e

    That tells the script to stop executing if it encounters an error.
    Next, we’ll delete the Xcode derived data. This is the equivalent of selecting Product -> Clean Build Folder from within Xcode.

    rm -rf ~/Library/Developer/Xcode/DerivedData

    Next we’ll run git clean.

    git clean -dfx

    The flag -d means to include directories, the flag -f initiates the deletion, and the flag -x instructs it to remove ignored files.
    Refer to the Git Documentation for an in-depth explanation on the flags. Make sure you don’t have any uncommitted changes that you want to keep. If you do, use git stash to save them for later.

    Now we’ll install CocoaPods. Confirm the location of your Podfile and add a command to navigate to that directory, and then execute the installation:

    cd ~/Location/of/your/project
    pod install

    Next, we’ll reset the Swift Packages caches:

    rm -rf ~/Library/Caches/org.swift.swiftpm
    rm -rf ~/Library/org.swift.swiftpm

    Then we’ll execute the resolvePackageDependencies command.

    xcodebuild -resolvePackageDependencies -workspace MyProject.xcworkspace -scheme MyProject

    Replace MyProject.xcworkspace with the name of your project. Note that in this example, since we’re using CocoaPods, we need to use .xcworkspace instead of .xcodeproj.

    Finally, we can have the script go ahead and open Xcode once it’s finished so we don’t have to keep watching the terminal for progress:

    open MyProject.xcworkspace

    Again, replace .xcworkspace with .xcodeproj if you’re not using CocoaPods.

    Currently, to execute the script you have to either cd to its directory or prepend the file path. We can use an alias to make a handy shortcut that will execute it with xcodereset from any directory. If you’re using Bash, open your .bash_profile file in a text editor and add this line:

    alias xcodereset="~/Path/to/directory/xcodereset.sh"

    Save the file and run source ~/.bash_profile in the terminal. Now you should be able to simply run xcodereset from any directory whenever you need to flush out all the build caches. Remember to close Xcode before running this so that Xcode doesn’t try to generate more build files while you’re in the middle of trying to clear everything.

    For reference, here is the complete file with statements added that print each step as it runs.

    #!/usr/bin/env bash
    
    set -e
    
    echo '**** Removing Xcode derived data... ****'
    rm -rf ~/Library/Developer/Xcode/DerivedData
    
    echo '**** Running git clean... ****'
    git clean -dfx
    
    echo '**** Running pod install... ****'
    cd ~/Location/of/your/project
    pod install
    
    echo '**** Resetting Swift Packages... ****'
    rm -rf ~/Library/Caches/org.swift.swiftpm
    rm -rf ~/Library/org.swift.swiftpm
    xcodebuild -resolvePackageDependencies -workspace MyProject.xcworkspace -scheme MyProject
    
    echo '**** Opening Xcode... ****'
    open MyProject.xcworkspace

    Leave a comment

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.