Cleaning your Xcode's garbage

germanxp

German

Posted on March 14, 2023

Cleaning your Xcode's garbage

Mobile devs out there with 128 or 256Gb SSDs Macbook: Hear me out!
If you find yourself struggling with the available free space on your disk, like Xcode not building or Terminal commands failing. All these happen because they can't write files to the SSD.
The good news are, I have a great solution for you:

Xcode-clean script
Shout out to Niklas Berglund

I found this script many years ago and I still use it, even when I am currently working on a 512Gb Macbook Pro.

Of course there are several less technical alternatives to do this, like cleaner apps, cache cleaning tools, etc.
But for some reason I tend to go with the nerdiest solutions.

For convenience you can create an alias like I did. Add the following line to your bash profile, oh-my-zsh, or whatever shell you use:

alias cleanxcode='~/xcode-clean.sh/./xcode-clean.sh'

Then, you can use it like:
> cleanxcode --help

Output:

Usage: /Users/.../xcode-clean.sh/./xcode-clean.sh [options]

Frees up disk space by removing Xcode data. NOTE: you might want to keep backups of the dSYM files in ~/Library/Developer/Xcode/Archives

EXAMPLE:
    /Users/.../xcode-clean.sh/./xcode-clean.sh -A

OPTIONS:
   -h           Show this help message
   -b [path]    Backup dSYM files to specified path before removing archives
   -a           Removed all Xcode archives
   -d           Remove everything in DerivedData folder
   -D           Remove everything in DeviceSupport folder
   -s           Remove simulator data
   -A           Remove all of the above(archived, DerivedData and simulator data)
   --dry-run    Dry run mode prints which directories would be cleared but don't remove any files
Enter fullscreen mode Exit fullscreen mode

A common use case for me is:
> cleanxcode -a -d -s

It wiped out a total of 9Gb this time 👍🏻

Clearing archives in /Users/.../Library/Developer/Xcode/Archives/* (freeing 25M disk space)
Clearing DerivedData content in /Users/.../Library/Developer/Xcode/DerivedData/* (freeing 7.6G disk space)
Clearing simulator data in /Users/.../Library/Developer/CoreSimulator/Devices/* (freeing 1.4G disk space)
Enter fullscreen mode Exit fullscreen mode

That is all for today.
I hope you find it as useful as I do.

💖 💪 🙅 🚩
germanxp
German

Posted on March 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Cleaning your Xcode's garbage