Git worktrees left by AI agents: which ones you can remove
Last checked 1 December 2026
A worktree whose pull request or branch is merged, with no unsaved changes, can be removed with git worktree remove and loses nothing. An open pull request or unmerged branch needs a look. One with unsaved changes would lose that work. Claude Code keeps them in .claude/worktrees, Codex in ~/.codex/worktrees.
Why worktrees pile up
A git worktree is a second working copy of a repository, with its own folder and its own checked-out commit, sharing the same history. AI agents use them so that several sessions can edit the same project without stepping on each other. Each session gets a worktree, the work gets merged, and the folder often stays on disk, with a full checkout and sometimes its own node_modules or build output.
Some agents clean up after themselves, but only in some cases:
- Claude Code removes the worktree of an unnamed session on exit when it is clean, and asks you when it holds work. Worktrees from non-interactive runs (
claude -p) are not cleaned up at all. - Codex keeps your 15 most recent managed worktrees by default, deletes older ones, and saves a snapshot of their work before it does.
Anything else, including worktrees you kept, worktrees from scripts and worktrees from other tools, stays until you remove it.
Where agents put their worktrees
| Path | Created by | Verdict | What you lose |
|---|---|---|---|
<repo>/.claude/worktrees/<name> | Claude Code (--worktree, subagents) | Depends on its state | See the next table |
~/.codex/worktrees | Codex app | Depends on its state | See the next table |
~/orca/workspaces | Orca | Depends on its state | See the next table |
<repo>/.worktrees | Other tools and scripts | Depends on its state | See the next table |
Codex uses $CODEX_HOME/worktrees, so the folder moves if you set CODEX_HOME or change the worktree root in its settings. To list every worktree of one repository, wherever it lives:
cd ~/path/to/your/repo
git worktree listWhich ones you can remove
What matters is not where a worktree is, but the state of its work.
| State of the worktree | Example | Verdict | What you lose |
|---|---|---|---|
| Its pull request was merged, nothing unsaved | feature-auth, PR merged | Safe | Nothing: the work is already in your main branch |
| Its branch is merged, nothing unsaved | worktree-bright-running-fox | Safe | Nothing: the work is already in your main branch |
| Its folder is already gone | listed as prunable | Safe | Nothing: only Git's leftover record of it |
| Its pull request is still open | PR in review | Review | The folder; the branch is kept, so the work is not lost |
| Its branch is not merged, or its PR was closed | an abandoned experiment | Review | The folder; the branch and its commits are kept |
| It has unsaved changes | changed or untracked files | Risky | That unsaved work, for good |
| A program is running inside it | a dev server, an open session | Never delete | Ouste skips it |
Codex creates its worktrees without a branch by default (a detached HEAD). Before removing one where commits were made, check that those commits are on a branch or already merged.
How to check one by hand
From inside the worktree's folder:
git status --short
git branch --show-current
git branch --merged main
gh pr list --head "$(git branch --show-current)" --state allThe first line lists unsaved changes: if it prints anything, stop there. The third line shows whether the branch is already in main (use your default branch's name). The last line needs the GitHub CLI and shows whether the pull request was merged, closed or is still open. Nothing above deletes anything.
How to remove one safely
Use git's own command rather than deleting the folder:
git worktree remove ~/path/to/the/worktreeGit's documentation says git worktree remove only removes clean worktrees, with no untracked files and no changes to tracked files, and refuses the others unless you add --force. That refusal is your safety net: do not add --force unless you know the unsaved work can go.
If you already deleted a worktree folder by hand, git still keeps a record of it. git worktree prune removes the records of worktrees whose folders are missing.
If git says the worktree is locked, an agent may still be using it. Claude Code locks the worktrees of running agents and background sessions so they cannot be removed mid-task.
What you should never delete
Never delete your main checkout, a worktree with unsaved changes you still want, or a worktree a program is running in. And never delete the .git folder of the main repository: every worktree depends on it.
For the rest of what agents leave on your Mac, see where AI coding agents store data on your Mac, what is in ~/.claude and what is in ~/.codex.
Doing it with Ouste
Ouste lists every worktree across your repositories, checks whether its branch is merged, whether its pull request is merged, closed or open when the GitHub CLI is signed in, and whether it holds unsaved changes. It classifies each one as Safe, Review, Risky or Protected with the sentence that says what you lose, and skips any worktree a program is running in. Worktrees are removed with git worktree remove, git's own command, so they do not go through Ouste's 7-day quarantine.
Questions
Where does Claude Code put its worktrees?
In .claude/worktrees/<name> at the root of your repository, on a branch named worktree-<name>.
Is it safe to remove a git worktree?
Yes if it has no unsaved changes and its branch or pull request is merged. git worktree remove refuses worktrees with unsaved changes unless you add --force.
Does removing a worktree delete the branch?
git worktree remove deletes the folder; the branch stays in your repository. Claude Code's own cleanup on exit can remove both, and asks you first when the worktree holds work.
I deleted a worktree folder by hand. What now?
Run git worktree prune in the repository. It removes git's records of worktrees whose folders are missing.