Free disk space from Claude Code and agent worktrees
Coding agents leave full git worktrees behind, often in .claude/worktrees. How to measure them, check each one is finished, and remove them without losing work.
Coding agents such as Claude Code, Cursor and Codex can run tasks side by side by giving each one its own git worktree: a second full checkout of your project. Claude Code keeps them inside the repository, usually under .claude/worktrees. Each worktree is a complete copy of your files, and once an agent installs packages or builds inside it, it also gets its own node_modules and build output. A few dozen of them for a large project can add up to tens of gigabytes. You can get the space back with git worktree remove, but only after checking each one has nothing unsaved and nothing unmerged.
This guide shows how to find them, how to tell which ones are finished, and how to remove them so the branch and its commits stay.
What a worktree is
A normal git repository has one working folder. git worktree add creates another folder that shares the same repository history but has its own checked-out branch and its own files. Agents use this so that several tasks can edit the same project at once without treading on each other.
The repository data in .git is shared, so commits are not copied. The files are. That is where the space goes: the source tree, plus anything the agent generated while working, such as installed dependencies, build folders and test output.
Worktrees also outlive the task. When an agent finishes, or you close its session, the folder often stays behind.
See every worktree and its size
In Terminal, go to the main checkout of your project and run:
git worktree list
This prints each worktree's path, the commit it is on and its branch. It only reads. Other tools may keep their worktrees in a different folder, so this list is the reliable way to find them all, not just the ones in .claude/worktrees.
To see how much space Claude Code's worktrees use, biggest last:
du -sh .claude/worktrees/* 2>/dev/null | sort -h
This also only reads. If you keep many projects in one folder, this finds every .claude/worktrees folder under it (replace ~/Projects with your code folder):
find ~/Projects -type d -path '*/.claude/worktrees' -prune -exec du -sh {} + 2>/dev/null | sort -h
Four checks before you remove one
Run these inside the worktree you are checking. Replace NAME with its folder name and main with your project's main branch if it is called something else.
cd .claude/worktrees/NAME
Nothing unsaved.
git status --porcelainNo output means no changed, new or deleted files. Any line means there is work that exists only in this folder.
Every commit is merged.
git rev-list --count main..HEADThis counts commits in the worktree that are not on
main.0means everything it made is already merged. You can also ask the question directly:git merge-base --is-ancestor HEAD main && echo mergedIt prints
mergedonly when the worktree's current commit is already part ofmain. Both commands only read.It is not locked. Back in the main checkout, run:
git worktree list --porcelainA worktree with a
lockedline was locked on purpose, often by the tool that made it. Leave it alone until you know why.Nothing is running inside it. Close the agent session that used it, and stop any dev server, watcher or test runner started there. To check from Terminal:
lsof +D .claude/worktrees/NAMENo output means no program has a file open in that folder. It only reads, but it can take a while on a large folder.
If a worktree has unmerged commits you still want, merge or push its branch first. If it has unsaved changes you do not need, discard them in that worktree, or commit them if you might.
Remove a finished worktree
From the main checkout:
git worktree remove .claude/worktrees/NAME
This deletes the folder and git's record of it. It does not use the Bin, so the folder's files are gone, but the branch and every commit on it stay in the repository. If the worktree has changed or untracked files, git refuses and tells you so, which is exactly the safety check you want.
The branch remains in git branch. When you no longer need it:
git branch -d BRANCH
-d only deletes a branch that is already merged. Git refuses otherwise.
Clear records for folders that are already gone
If some worktree folders were deleted by hand, git still lists them. See what would be cleared:
git worktree prune -n -v
Then clear them:
git worktree prune
prune only removes git's records for folders that no longer exist. It never deletes a folder that is still on disk.
Why not rm -rf the whole folder
It is tempting to delete .claude/worktrees in one go. Don't:
- Unsaved work is lost for good. Changes the agent made but never committed live only in that folder.
rm -rfskips the Bin, so there is no getting them back. - Detached commits can be lost. Commits on a branch survive because the branch is in the shared repository. But a worktree on a detached HEAD has no branch pointing at its commits, and git eventually throws such commits away.
- Files git ignores go too. Agents sometimes copy
.envfiles or create local databases inside a worktree. They are not in git, so nothing else has them. - Git is left with stale records. The repository still lists the missing worktrees, and their branches stay checked out, until you run
git worktree prune.
git worktree remove checks for the first problem for you and refuses a dirty worktree unless you add --force. Only add --force once you have read git status and are sure you want to throw those changes away.
Stop them piling up
- Remove a worktree when you merge its task, while you still remember what it was for.
- Run
git worktree listin your busiest projects every week or so. - Agent data in your home folder grows too. The ~/.claude folder page explains which parts you can clear.
Each worktree can also hold its own copy of your dependencies, so the node_modules guide is worth a read alongside this one.
Do it with Headroom
Headroom's Clean scan looks for worktrees in the .claude/worktrees folder of projects in your usual code folders, such as Developer, Projects, Code, Documents and Desktop. For each one it checks that there is nothing unsaved and that every commit is already on the main checkout's branch. It leaves out worktrees that are locked, that a program is working in, that were touched in the last 12 hours, or that belong to the Claude desktop app's own sessions.
- Finished agent worktrees have nothing unsaved and nothing unmerged. Headroom removes them with
git worktree remove, so the branch and its commits stay. - Agent worktrees with open work are listed with their size, so you can see where the space went. One with unsaved changes stays blocked until you commit or discard them in the project. One with unmerged commits is unticked, so you can merge or discard it first.
The scan is free. A $19 one-time licence, for up to three Macs, clears what you tick. This check is in the download edition; Headroom Lite does not run git. See the price or download Headroom.

Using an agent to hunt for space in the first place? Start with how to free up Mac storage with Claude Code, safely.
Questions
Can I delete the .claude/worktrees folder?
Not with rm -rf. Each folder inside is a git worktree that may hold changes you have not committed. Check each one with git status, then remove it with git worktree remove from the main checkout, which refuses if anything is unsaved.
Does git worktree remove delete the branch?
No. It removes the folder and git's record of it. The branch and all its commits stay in the repository, so you can check it out again or merge it later. Delete the branch separately with git branch -d once it is merged.
Why are my Claude Code worktrees so big?
A worktree is a full checkout of your project. Once an agent installs dependencies or builds inside it, it also gets its own node_modules and build output, so each copy can be as large as the original project.
How do I see all the worktrees in a repository?
Run git worktree list in the repository. It prints every worktree with its path, the commit it is on and its branch. Add --porcelain to also see which ones are locked or can be pruned.
What does git worktree prune do?
It clears git's records of worktrees whose folders no longer exist, for example after a folder was deleted by hand. It never deletes a folder that is still there. Run git worktree prune -n first to see what it would clear.