How to Safely Clear DerivedData in Xcode

How to Safely Clear DerivedData in Xcode

Written by

in

Quick answer: Yes, you can safely delete the contents of ~/Library/Developer/Xcode/DerivedData. Xcode stores temporary build artifacts, module caches, and index files there, and it will automatically regenerate everything the next time you build your project.

Why this happens

When you compile an app in Xcode, the build system does not write intermediate files directly into your source code repository. Instead, Xcode isolates these temporary artifacts inside the ~/Library/Developer/Xcode/DerivedData directory.

This folder holds critical temporary workspace data, including:
– Precompiled headers and intermediate object files (.o)
– Swift module caches and interface files
– SourceKit indexing databases that power autocompletion and syntax highlighting
– Build logs and compilation diagnostics
– Compiled binary executables and debug symbol files (.dSYM)

Because Xcode assigns each project a unique hashed directory name inside DerivedData, every branch switch, workspace rename, or toolchain update can leave orphaned files behind. Over weeks or months of active development, this folder routinely expands from a few hundred megabytes to 20 GB, 50 GB, or even 100 GB+.

Nothing in DerivedData is irreplaceable. It contains zero source code, zero assets, and zero configuration files. Deleting it is completely safe; the only side effect is that your next build will take longer because Xcode must rebuild targets and rebuild its indexing database from scratch.

How to fix it

1. Delete DerivedData directly from the Terminal

The fastest, most reliable way to wipe all cached build artifacts across all projects is via the command line. Before running the command, quit Xcode to ensure no file locks interfere with the removal process.

Run the following command in Terminal:

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

To verify how much space you reclaimed, you can check the size of the directory before and after deletion using du:

du -sh ~/Library/Developer/Xcode/DerivedData

2. Locate and remove files through Xcode Settings

If you prefer a graphical interface or want to inspect specific project folders before deleting them, use Xcode’s built-in location manager:

  1. Open Xcode.
  2. In the menu bar, select Xcode > Settings… (or Xcode > Preferences… on older macOS versions), or press Command + ,.
  3. Click the Locations tab.
  4. Under the Derived Data label, you will see the active directory path alongside a small gray arrow icon.
  5. Click the arrow icon next to the path. This opens ~/Library/Developer/Xcode/DerivedData directly in Finder.
  6. Select all subfolders inside DerivedData (or specific project folders matching your project name) and press Command + Delete to move them to the Trash.
  7. Empty your Mac’s Trash to reclaim the disk space.

3. Clear DerivedData for a single project only

If you are troubleshooting an indexing bug or compilation error in one repository, you do not need to purge the cache for every app on your machine. Xcode names each subfolder using your project name followed by an alphanumeric hash (for example, MyApp-abcd1234efgh5678ijkl).

You can target only that project folder in Terminal:

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

Alternatively, locate the project folder in Finder using the steps from Method 2 and delete only the folder that starts with your target app’s name.

4. Create a permanent Terminal alias for one-command cleanup

Because corrupted cache files are a frequent cause of phantom compiler errors and stale autocomplete failures, developers often clear this directory regularly. You can create a shell alias so you never have to type the full path again.

Open your shell profile (typically ~/.zshrc on modern macOS versions):

nano ~/.zshrc

Add the following function or alias to the bottom of the file:

alias clearderived='rm -rf ~/Library/Developer/Xcode/DerivedData/* && echo "DerivedData cleared."'

Save the file (Control + O, then Enter), exit nano (Control + X), and reload your shell configuration:

source ~/.zshrc

Now, whenever you need to clear the build cache, simply run:

clearderived

5. Perform a Clean Build Folder inside Xcode

If you only need to discard intermediate files for your currently open workspace without touching global caches or other projects, use Xcode’s internal clean command.

  1. In Xcode, press Shift + Command + K (or select Product > Clean Build Folder from the menu).
  2. For an even deeper clean that purges all build products for the active scheme, hold the Option key while opening the Product menu and click Clean Build Folder….

Note that while Clean Build Folder removes build outputs for the active scheme, it may leave behind stale indexing files and module caches in DerivedData. If you are trying to resolve compiler crashes or reclaim disk space, removing the physical directory via Method 1 or Method 2 is required.

How to prevent it

  • Inspect Xcode Storage settings: In modern versions of Xcode, open Xcode > Settings > Storage to view disk usage for Derived Data, Archives, and Simulator runtimes, and purge unused caches with one click.
  • Use a dedicated .zshrc alias: Run your cleanup alias after closing completed projects or before switching between major Xcode releases.
  • Avoid custom DerivedData locations: Keep the location setting at “Default” in Xcode > Settings > Locations so that cache files do not clutter your root source folders or get committed to Git.
  • Maintain clean .gitignore files: If you use command-line build tools such as xcodebuild, ensure build/ and DerivedData/ entries are always included in your global or project .gitignore.

FAQ

Will deleting DerivedData delete my project files or code?

No. The DerivedData directory contains only generated build artifacts, indexing databases, logs, and temporary module caches. Your source files, assets, project settings, and Git history remain untouched in your project directory.

Why does Xcode compile slower after I clear DerivedData?

Because you removed all cached object files, precompiled headers, and module maps, Xcode must execute a full build from scratch rather than an incremental build. It must also reindex your codebase for syntax highlighting and autocompletion, which temporarily increases CPU usage.

What is the difference between Clean Build Folder and deleting DerivedData?

Clean Build Folder (Shift + Command + K) deletes only the compiled outputs and intermediate artifacts for the currently active scheme and project. Deleting the DerivedData folder purges everything across all configurations, including indexing indexes, logs, and caches for inactive or deleted projects.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.