Blog · Guide
How to clean up the git worktrees your coding agents leave behind
List every worktree with git worktree list, measure each one with du -sh, and remove the finished ones with git worktree remove <path>, then delete their merged branches. git worktree prune only clears git's records of folders that are already gone; it deletes nothing on disk. Claude Code, Codex and Cursor each sweep only their own worktrees, each by its own rule, so the rest is up to you.
Running agents in parallel means one worktree per agent. If you followed How to run multiple Claude Code agents in parallel or Run multiple Codex CLI agents in git worktrees, you have made a lot of them. This post is about getting rid of them.
Why this keeps happening
Agents make worktrees faster than people delete them. Through October and November 2026, developers kept posting about disks filled by forgotten agent worktrees. One found six old Claude Code worktrees, most of their size in node_modules, with most of their branches already merged. Another counted 41 worktrees across Claude Code, Codex and T3 Code taking tens of gigabytes. A third had a Mac running out of space, with dozens of worktrees each holding its own dependencies and build output. A thread on r/ClaudeCode asks the same question, and people have built one-off cleaners for it, such as Lopper and Repobs.
Why do agent worktrees take so much disk?
Not because of git. All worktrees of a repository share one object store, so the history isn't copied; replies in the Reddit thread make the same point. A worktree costs its checked-out files plus whatever got installed or built inside it: node_modules, Rust's target/, .next, Xcode's DerivedData. Each agent installs and builds in its own folder, so ten worktrees can mean ten copies of your dependencies and ten build caches.
How do I find every worktree and what it costs?
Start from git, not from the file system. Git knows every worktree of the repository, wherever an agent put it:
cd ~/code/app
git worktree list
git worktree list --porcelain # one field per line, with branch and lock state
Then measure them. Pass the folders from the list to du:
git worktree list --porcelain | awk '/^worktree /{print $2}' | xargs -I{} du -sh {}
Paths with spaces need more care than that one-liner gives them. Where each tool puts its worktrees by default:
| Tool | Default location |
|---|---|
| Claude Code | .claude/worktrees/<name> inside the repository (docs) |
| Codex | $CODEX_HOME/worktrees (docs) |
| Cursor | Its own worktree root; the docs don't name the path, but git worktree list shows it |
| Zoes | ~/.zoes/worktrees/<project>/<name> (docs) |
Agents also keep their own state outside the repository, in folders such as ~/.claude and ~/.codex. One post lists those places next to the worktrees themselves.
How do I remove a worktree safely?
A worktree can hold work that exists nowhere else. Check before you remove it.
- Look for uncommitted files:
git -C ../app-fix-auth status --short - Look for commits that are on no remote:
git log fix-auth --not --remotes --onelineEmpty output means every commit on the branch has been pushed. For a worktree on a detached HEAD, run
git -C <path> log --oneline HEAD --not --remotesinstead. - Remove the worktree. This deletes the folder and git's record of it:
git worktree remove ../app-fix-authGit refuses if the worktree has modified or untracked files.
--forceoverrides that. A locked worktree needsgit worktree unlock <path>first, or--forcegiven twice (git docs). - Delete the branch if it's merged.
-drefuses an unmerged branch, which is what you want:git branch -d fix-auth - Prune records of folders that are already gone:
git worktree prune
Two shortcuts to avoid
rm -rfon the folder. The disk space comes back, but git still lists the worktree until you rungit worktree prune, and still treats its branch as checked out. Usegit worktree remove.- Symlinking
node_modulesinto each worktree. It saves disk, but a.gitignoreline ofnode_modules/, with the trailing slash, matches only a directory, not a symlink. One r/ClaudeCode user found that out aftergit add -Aand lost a data folder. Cursor's worktree docs also advise against symlinking dependencies.
To make each worktree cheaper instead, the Reddit thread suggests pnpm, whose store hard-links packages into each project instead of copying them.
What does each agent clean up on its own?
Every tool cleans only the worktrees it made, and each uses a different rule. From each tool's docs, checked 19 November 2026:
| Tool | When it removes worktrees | What it protects |
|---|---|---|
| Claude Code | On exit of an interactive --worktree session: a clean unnamed worktree goes, with its branch; a named one prompts; one with work asks keep or remove. A periodic sweep removes subagent and background-session worktrees older than cleanupPeriodDays. | The sweep skips worktrees with changes, untracked files or unpushed commits, and worktrees you made with git worktree add. -p runs get no exit prompt and are never cleaned up; their lock stays until a later session's stale-lock sweep. |
| Codex app | When you archive a chat, or when there are more than the 15 most recent Codex-managed worktrees (the default; change it or turn it off in settings). | Never while a pinned chat uses the worktree or a chat is in progress. Permanent worktrees stay. It saves a snapshot first and offers to restore it when you reopen the chat. Its worktrees are on a detached HEAD, with no branch. |
| Cursor 3.5+ | Every cursor.worktreeCleanupIntervalHours (default 6), keeping the newest up to cursor.worktreeMaxCount (default 25) per machine, across all workspaces. | Only the newest worktrees up to the cap. Worktrees in its root that it didn't make, for example with git worktree add, count and can be removed too. |
| Zoes | Only when you delete one: from the sidebar, or after merging its pull request. No sweep by age or count. | The confirm tells you what you'd lose. Details below. |
| Plain git | Never. Worktree folders stay until you remove them. | git worktree remove refuses unclean or locked worktrees without --force. |
If you run more than one of these tools on a repository, none of them sees the others' worktrees as its own. git worktree list is still the one place that shows all of them.
How does Zoes handle worktree cleanup?
Zoes makes new worktrees in ~/.zoes/worktrees/<project>/<name>. To use another folder, open Settings › Projects and change Worktree folder under Locations.
Deleting a worktree
Right-click the worktree in the sidebar, or open its ··· menu, and click Delete worktree…. The confirm names the folder it deletes and the terminals it closes, and warns how many uncommitted files and how many commits on no other branch would be lost.
- Also delete branch removes the local branch too.
main,masterand a detached HEAD never go, and remote branches are never deleted. - The project's main worktree can't be deleted.
- When you merge a pull request from Zoes's pull request card, Delete the worktree after merging (on by default) removes the worktree once the merge lands and keeps the branch. A merged pull request whose worktree is still there offers Keep or Delete worktree. If the worktree has uncommitted files, Zoes asks first.
Teardown before the folder goes
Before it removes a worktree, Zoes runs a teardown, the first it finds of: When one is deleted in the project's settings, the teardown list in .zoes/config.json, or .zoes/teardown.sh. It runs in the worktree folder, so a docker compose down still finds its files. It gets ZOES_ROOT_PATH, ZOES_WORKSPACE_PATH and ZOES_WORKSPACE_NAME. If it fails, Zoes shows Couldn't tear down <name> with the end of its output and a Delete anyway button.
Settings › Projects › Deleting a worktree holds three settings:
- Also delete its branch, for the local branch only.
- Worktree files: Move to Trash or Delete permanently.
- Teardown script timeout, 120 s by default.
Worktrees made elsewhere
If git has worktrees of the project that the sidebar doesn't list, such as ones an agent made on its own, the project's menu offers Import n worktrees. A worktree you delete outside the app is forgotten once both git and the disk have lost it.
Making each worktree cheaper
Setup commands in .zoes/config.json or .zoes/setup.sh run when Zoes makes a worktree, and teardown runs when it deletes one:
{
"setup": ["pnpm install"],
"teardown": ["docker compose down"]
}
Zoes's own repository uses a .anywhere/setup.sh that clones target/ with APFS copy-on-write (cp -c) and symlinks the gitignored third_party/, so a new worktree doesn't rebuild every dependency.
What Zoes doesn't do: it doesn't measure disk use, and it doesn't delete worktrees by age or count. For that, use the commands above. The full reference is Start an agent in a new worktree.
Zoes is free and MIT licensed, and runs the Claude Code and Codex you already have.
Star Zoes on GitHub Download for MacFAQ
Does git worktree prune delete worktree folders?
No. git worktree prune only removes git's records of worktrees whose folders are already gone. To delete a worktree's folder and its record together, use git worktree remove <path>.
How do I remove a locked worktree?
Run git worktree unlock <path>, then git worktree remove <path>. You can also pass --force twice to remove a locked worktree, but first check why it is locked. Claude Code, for one, locks a worktree while its agent runs.
Does Claude Code delete worktrees automatically?
Partly. When you exit an interactive --worktree session, Claude Code removes a clean unnamed worktree and its branch, and asks about the rest. Worktrees from -p runs are never cleaned up on exit. A periodic sweep removes subagent and background-session worktrees older than cleanupPeriodDays, but skips any with changes, untracked files or unpushed commits, and skips worktrees you made with git worktree add.
How many worktrees does Codex keep?
The Codex app keeps the 15 most recent worktrees it manages by default. You can change the limit or turn cleanup off in its settings. It doesn't delete a worktree that a pinned or running chat uses, or a permanent worktree, and it saves a snapshot before it deletes one.
Will deleting a worktree delete my branch?
With git, no: git worktree remove leaves the branch, and you delete it with git branch -d. In Zoes, only if you select Also delete branch, and even then main, master and remote branches stay.