How to safely delete node_modules and Xcode DerivedData

node_modules and DerivedData are rebuildable, but not all of it is equal. How to find the big ones, what's safe to delete and how to get it back.

If you write code on a Mac, a big share of your disk isn't your code at all. It's what your tools generate from it: dependencies, build products, indexes, simulators. All of it can be rebuilt, which is exactly why it piles up — nothing ever tells you it's safe to let go.

The two usual suspects are node_modules (one full copy of your dependencies per JavaScript project) and Xcode's DerivedData (build products and indexes for every project and scheme you've opened). Neither is precious. Both can still cost you an afternoon if you delete the wrong thing at the wrong time. Here's how to do it calmly.

Find the big ones first

Measure before you delete. In Terminal:

# Xcode's build products and indexes
du -sh ~/Library/Developer/Xcode/DerivedData

# Every node_modules under your code folder, biggest first
# (change ~/Code to wherever you keep projects)
find ~/Code -type d -name node_modules -prune -exec du -sh {} + 2>/dev/null | sort -hr | head -20

The -prune stops find from descending into nested node_modules, so each project is counted once. On a busy disk this takes a minute.

Xcode: what you can delete and what to keep

Folder Safe to delete? What it costs you
~/Library/Developer/Xcode/DerivedData Yes The next build and indexing take longer
~/Library/Developer/Xcode/iOS DeviceSupport Yes Xcode rebuilds it the next time you connect that device
Simulators for iOS versions you no longer have Yes Nothing — they can't run anyway
~/Library/Developer/Xcode/Archives Keep the ones you've shipped Without them you can't symbolicate crash reports

DerivedData. Quit Xcode first. Then open Finder, press Shift‑Cmd‑G, paste ~/Library/Developer/Xcode/DerivedData, select everything inside and move it to the Trash. If you only want to clean one project, Product → Clean Build Folder (Shift‑Cmd‑K) in Xcode does that.

Simulators. This removes the ones tied to runtimes that are no longer installed:

xcrun simctl delete unavailable

Old simulator runtimes themselves are managed in Xcode → Settings → Components, where each one shows its size and can be removed.

Archives are the exception. Each one holds the debug symbols for a build you sent to testers or the App Store. Delete the archives of builds nobody runs anymore, and keep the rest.

node_modules: the one rule that matters

You can delete a project's node_modules safely when the project has a lockfile: package-lock.json, yarn.lock, pnpm-lock.yaml or bun.lockb. The lockfile records the exact version of every package, so you get the same tree back with:

npm ci                              # npm
yarn install --frozen-lockfile      # Yarn classic
pnpm install --frozen-lockfile      # pnpm

Without a lockfile, reinstalling may pull newer versions than the ones you had. Run an install once to create the lockfile and commit it before you clean.

A few things that make reinstalling less smooth than it sounds:

  • Files edited by hand inside node_modules are gone. If you patch a dependency, use patch-package or your package manager's patch support so the fix lives outside it.
  • Private registries need your auth token in .npmrc to download again.
  • Native modules compile on install and need the Xcode Command Line Tools.

For many old projects at once, the open-source npx npkill lists every node_modules with its size and lets you pick which to remove.

Package manager caches are a separate pile, shared by all projects: npm keeps one in ~/.npm, pnpm has its store and Yarn its cache. pnpm store prune removes packages no project uses anymore; clearing the others means the next installs download everything again.

The same idea in other stacks

  • Rust: target/ holds build output; cargo clean removes it for a project.
  • Swift packages: the .build/ folder is rebuilt by swift build.
  • .NET: bin/ and obj/ are regenerated on the next build.
  • Python: a virtual environment (.venv) can be recreated from requirements.txt or your lockfile.
  • Docker keeps images and build cache in its own disk image. We covered it in "System Data" explained.

A routine that doesn't hurt

  1. Commit and push before you clean. Build output is never the problem; uncommitted work is.
  2. Start with projects you haven't opened in months. That's where most of the space is, and you won't miss their build output.
  3. Leave the project you're working on this week alone, unless you need the space right now. Rebuilding costs time you'd rather spend on the deadline.
  4. Reinstall when you come back, not before.

Quick answers

Will deleting DerivedData break my project? No. It only holds things Xcode generates. Your project files, settings and source code live elsewhere.

Does deleting node_modules touch my source code? No. Your code, package.json and the lockfile stay where they are.

Why does it all come back? Because you keep building. That's fine — clean old projects every few months, not your active one every week.

How Depuria does it

Depuria is a free, native Mac app built around this routine:

  • Dev Cleanup understands Node, Rust, .NET, Swift and Python projects. It finds the rebuildable build output — node_modules, target, .build, obj/bin and more — project by project, and only that goes, to the Trash. Your source stays untouched.
  • Old Projects finds finished projects you haven't opened in months, ranked by size. It can archive one as a .zip next to the folder and move the original to the Trash.
  • Docker images and build cache are freed too; volumes, where your databases live, are measured and never deleted.

Nothing is removed without your confirmation.