Understanding and Managing Xcode Space

Learn how to free up space hogged by Xcode in caches, derived data, archives and simulators! By Keegan Rush.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Supporting Caches

Two other caches of note are those of Carthage and CocoaPods. These dependency managers help you to manage third-party libraries that you may be using in your apps.

If you use Carthage, you’ll find its cache at ~/Library/Caches/org.carthage.CarthageKit.

CocoaPods has a special command you can use to clear its cache. To clear the CocoaPods cache, run this in the terminal:

pod cache clean --all

Using the dedicated cache clean command is better than manually deleting a folder. This is because if CocoaPods changes where it stores its cache, the command will still work for the new location. That’s great news if you decide to write a script to clear your caches!

In fact, in the next section, you’ll do just that, tying together everything you’ve learned so far in the ultimate spring-cleaning script!

Tying it All Together

Rather than trying to remember where to go to free some data, it can be much easier to codify each step into an automation script and then run that script when you’re short on space.

It’s important that an automated solution doesn’t do anything too destructive. You need to be cautious when clearing certain folders, like your archives.

For caches and other folders that don’t need a human touch, a script is a fitting solution.

Creating a Script

First, open a Terminal window. Run this command:

cd ~/Documents && touch clean-xcode.sh

cd and touch command

This changes the terminal window’s directory to your Documents folder. Afterward, it creates a new, blank script.

Note: You may be asked to give permission for Terminal to access files in your documents folder. If you are asked, please do so.

Open the empty script in your favorite text editor. Then, type this:

#!/usr/bin/env bash

# 1
echo "Removing Derived Data..."
rm -rf ~/Library/Developer/Xcode/DerivedData/

# 2
echo "Removing Device Support..."
rm -rf ~/Library/Developer/Xcode/iOS\ DeviceSupport
rm -rf ~/Library/Developer/Xcode/watchOS\ DeviceSupport
rm -rf ~/Library/Developer/Xcode/tvOS\ DeviceSupport

# 3
echo "Removing old simulators..."
xcrun simctl delete unavailable

# 4
echo "Removing caches..."
rm -rf ~/Library/Caches/com.apple.dt.Xcode
rm -rf ~/Library/Caches/org.carthage.CarthageKit

# 5
if command -v pod  &> /dev/null
then
    # 6
    pod cache clean --all
fi

echo "Done!"

At the beginning of the file is a shebang, which tells your computer how to execute the script. The shebang always has to be the first line of the script.

The rest of the file is the culmination of everything you’ve learned so far. Here’s what’s happening, step by step:

  1. First, echo prints out a message to the terminal, so you know how the script is running. Then, rm deletes the derived data folder.
  2. Print another status update, then delete each device support folder. If the folders don’t exist, nothing happens.
  3. Delete unavailable simulators.
  4. Delete the caches for Xcode and Carthage, if they exist.
  5. Check to see if CocoaPods is installed on this computer.
  6. If CocoaPods is installed, clean the CocoaPods cache.

Next, save the file. In TextEdit, do this by clicking File ▸ Save in TextEdit’s menu bar.

You’ve created your script, and you’re almost ready to try it out.

Running the Script

If you try to run clean-xcode.sh as-is, you’ll get an error:

Denied permission error when running script

That’s because your computer sees it as a text file, not something you can execute like a script or program. To make it executable, you need to run a command in Terminal.

In your Terminal window, run this:

chmod u+x clean-xcode.sh

chmod command

Now, your computer will recognize clean-xcode.sh as an executable script.

Note: If you’re not prepared to delete your derived data, device support or caches, don’t run the script yet. You can still read along for the rest of the section.

Finally, you can run clean-xcode.sh by specifying its path in the terminal:

./clean-xcode.sh

The script executes, printing out status reports as it cleans out derived data, device support, simulators and caches.

Script output

With one command, you’ve reclaimed some of the storage that Xcode builds up over time. Great job!

Where to Go From Here?

For a completed version of clean-xcode.sh, download the final project by clicking the Download Materials button at the top or bottom of this tutorial.

In this tutorial, you learned some ways to reclaim some of your storage that Xcode claimed for itself. You learned when you’d want to clear each location and when you should be more careful about deleting certain files. You also gained some understanding of how Xcode’s caches and intermediary data can affect compilation issues.

To learn more about how Xcode works along the whole build process and get your feet wet with some more scripts for build automation, take a look at iOS App Distribution & Best Practices.

I hope you’ve freed up some space on your Mac in this tutorial, and learned some tips and tricks along the way. If you have any questions or comments, please join the forum discussion below!