ai newsMi., 25. Nov.
EilmeldungYale’s Sonnenfeld: Nvidia is a ‘detriment’ to U.S. industrial might — not a champion

Claude Code Worktrees Filling Your Disk? How to Find and Remove Leftover Git Worktrees

Note: Originally published at getcleanbit.app . Written by the CleanBit developer with AI assistance. All measurements were taken on a real developer Mac. Claude Code and Codex worktrees fill up your Mac because Git shares commit history across worktrees, but never shares untracked build directorie

dev.toMittwoch 01:18 · 25.11.6 Min. Lesezeit
Bild: dev.to

Posted on Nov 25 • Originally published at getcleanbit.app • AI-assisted

Note: Originally published at getcleanbit.app. Written by the CleanBit developer with AI assistance. All measurements were taken on a real developer Mac.

Claude Code and Codex worktrees fill up your Mac because Git shares commit history across worktrees, but never shares untracked build directories like node_modules, Rust target/, Swift.build/, or Python.venv/. Every parallel agent task that runs npm install, cargo build, or swift build inside its own git worktree creates a fresh multi-gigabyte copy of dependencies—and those folders stay on disk even after your PR is merged unless you remove the worktree with git worktree remove or clear its build directories.

On one developer Mac we measured (read-only du across the home folder, excluding ~/Library ), project build artifacts alone reached 66.3 GB across 304 node_modules folders, 5.1 GB across 8.venv folders, and 2.9 GB across 7.build folders —alongside ~/.claude (1.9 GB), ~/.local/share/claude (902 MB), and ~/.codex (5.2 GB). Heavy parallel-agent users hit much steeper spikes: developers on r/ClaudeCode and r/ClaudeAI have reported leftover agent worktrees growing a single workspace to 95 GB, and parallel Rust agent checkouts generating 10–40 GB target/ folders each until the startup disk hit 95%+ full.

Before deleting anything, measure where agent worktrees and their build folders actually sit on your Mac. The commands below are strictly read-only.

# Total disk usage in common agent worktree directories (read-only) du -sh \ ~/.claude/worktrees \ ~/.codex/worktrees \ ~/.cursor/worktrees 2 > /dev/null # Largest individual worktree folders inside ~/.claude/worktrees and ~/.codex/worktrees du -sh ~/.claude/worktrees/* ~/.codex/worktrees/* 2 > /dev/null | sort -h | tail -20 Enter fullscreen mode Exit fullscreen mode List every Git worktree registered by a repository Claude Code and other tools can also create worktrees inside a hidden folder inside the repo itself (such as <repo>/.claude/worktrees ) or in a sibling folder next to your repo. Inside any repository, run:

cd ~/Repos/your-project git worktree list Enter fullscreen mode Exit fullscreen mode To see whether any worktrees are locked, detached, or already missing from disk ( prunable ), add --porcelain:

git worktree list --porcelain Enter fullscreen mode Exit fullscreen mode Find the biggest node_modules, target, and.build folders inside worktrees find ~/.claude/worktrees ~/.codex/worktrees ~/.cursor/worktrees \ \ ( -name node_modules -o -name target -o -name.build -o -name.venv \ ) \ -type d -prune -exec du -sh { } + 2 > /dev/null | sort -h | tail -25 Enter fullscreen mode Exit fullscreen mode Note: du shows total directory size, not how much is safe to delete blindly. A worktree may still contain uncommitted edits or an unmerged branch—always verify Git status before removing the checkout itself.

If you want a quick read-only audit script covering Xcode, node_modules, AI coding tools, and user caches together, you can also generate one with the free CleanBit Disk Check audit generator.

Different AI coding tools store isolated worktrees in different places, and each location behaves slightly differently when you audit or clean it:

Never start with rm -rf on a worktree directory. Removing a worktree folder directly leaves stale metadata in.git/worktrees/ and gives you zero warning if an agent left uncommitted edits behind.

For each worktree path printed by git worktree list, inspect its working tree before touching it:

# Check uncommitted or untracked files inside a specific worktree git -C /path/to/worktree status --short # From your main repo, see which branches have NOT been merged into main git branch --no-merged main Enter fullscreen mode Exit fullscreen mode If git status --short shows changes you want to keep, commit them or create a patch before removing the worktree.

git worktree remove refuses to delete a worktree that has untracked or modified files unless you pass --force, making it much safer than rm -rf:

# Run from your main repository directory git worktree remove /path/to/old-worktree Enter fullscreen mode Exit fullscreen mode If Git refuses because the only leftover files are disposable build caches (such as node_modules or target ) and you have already confirmed no source edits exist, remove the build folder first or pass --force only after verifying git -C /path/to/old-worktree status:

# Only after confirming no wanted source changes remain git worktree remove --force /path/to/old-worktree Enter fullscreen mode Exit fullscreen mode Step 3: Clean up stale Git metadata with git worktree prune If you (or a coding agent) already deleted a worktree folder manually in Finder or with rm -rf, Git keeps dead administrative entries under.git/worktrees/. Preview what Git would prune first with --dry-run:

# Preview stale worktree references without changing anything git worktree prune --dry-run --verbose # Remove stale administrative entries git worktree prune Enter fullscreen mode Exit fullscreen mode Step 4: Delete merged temporary branches safely Use lowercase -d (never -D blindly)—Git will refuse to delete a branch if its commits are not yet merged:

git branch -d claude/old-feature-branch Enter fullscreen mode Exit fullscreen mode Option B: Keep the worktree, delete only its build folder If you want to keep a paused worktree branch around for reference but reclaim the gigabytes immediately, delete only the regenerable build directory inside it:

# Example: clear Rust build output or Node dependencies inside a paused worktree rm -rf /path/to/paused-worktree/target # or rm -rf /path/to/paused-worktree/node_modules Enter fullscreen mode Exit fullscreen mode If your JS/TS projects use pnpm, also run pnpm store prune afterward so unreferenced hard-linked packages are freed from ~/Library/pnpm.

Warning: Avoid telling an AI coding agent to "clean up my disk" with shell access. Developers on r/ClaudeCode have reported agents misclassifying source directories as disposable build caches and wiping them with rm -rf. Keep disk cleanup read-only first, use git worktree remove for checkouts, and prefer moving build folders to the Trash so you can put them back if needed.

I'm the developer of CleanBit, a sandboxed Mac App Store disk space cleaner for macOS 15+. If you want to reclaim gigabytes from duplicated node_modules, Rust target/, Swift.build/, and Python.venv/ folders across your projects and worktrees without risking source code:

Scanning and space analysis are free, and your first 2 cleans are free on the Mac App Store (optional $4.99/mo, $19.99/yr, or $29.99 one-time lifetime purchase).

Git worktrees share your repository's.git object database, so source code and commit history take almost no extra space. The bloat comes from untracked dependencies and build caches—each parallel worktree installs its own node_modules, Rust target/, Swift.build/, or Python.venv/, multiplying disk usage by the number of active or forgotten worktrees.

Yes. git worktree remove <path> refuses to delete a worktree if it has uncommitted changes or untracked files, and it cleans up the matching registration inside.git/worktrees/. Running rm -rf bypasses Git's safety checks and leaves stale worktree metadata behind until you run git worktree prune.

git worktree prune does not delete live working directories or source files. It only removes stale administrative metadata files inside.git/worktrees/ that point to worktree folders which were already deleted or moved manually. You can preview its actions safely with git worktree prune --dry-run --verbose.

Yes, as long as no dev server, watcher, or coding agent is currently running in that worktree. Removing only node_modules, target/, or.build/ keeps your branch, uncommitted source files, and Git worktree intact—you just re-run npm install or cargo build if you return to that worktree later.

CleanBit's Build Artifacts module scans ~/.claude/worktrees, ~/.codex/worktrees, and visible home folders for disposable build directories ( node_modules, target,.build,.venv, etc.), parses.git gitdir: pointers to skip Git-tracked files, and moves selected build folders to the Trash. It does not remove Git worktree checkouts or branches themselves, and currently does not auto-scan ~/.cursor/worktrees or hidden repo subfolders like <repo>/.claude/worktrees.

Original guide with interactive table of contents & related Mac storage guides: https://getcleanbit.app/guides/claude-code-worktrees-disk-space-mac/

Quelle: dev.to (dev.to). Original öffnen