How to
Clean up Claude Code, Codex and Cursor worktrees without losing work
Find the git worktrees your coding agents left behind, check each for unpushed or uncommitted work, and remove them with git worktree remove and prune.
Free to scan. Seven days of cleaning free, no card. macOS 26 or later.
Coding agents give each task its own git worktree, a separate checkout on its own branch. Each one usually gets its own node_modules or .venv, too. Run a few dozen tasks and you have a few dozen checkouts.
Most of them are safe to remove. A few hold the only copy of some work. Here is how to tell which is which, and how to remove them so git doesn't keep stale records.
This page is the manual check. DevSweeper's agent worktrees page is how the app finds these folders and rates them.
Where each agent puts them
- Claude Code:
claude --worktree <name>creates.claude/worktrees/<name>/at your repository root, on a branch namedworktree-<name>. - Codex: the Codex app manages its own worktrees and deletes older ones automatically, keeping a limited number. The Codex worktrees docs give the current limit and which ones it keeps.
- Cursor: cleans up older worktrees on its own and keeps the newest ones, up to a maximum you can change in its settings. See Cursor's worktrees docs.
So Codex and Cursor clean up after themselves within limits, and Claude Code cleans up when a session ends (more on that below). Anything a crash, a killed terminal or a "keep it" answer left behind is still there.
1. List what git knows about
From inside the repository:
git worktree list
Each line is a path, a commit and a branch. git worktree list -v also shows the worktrees that are locked or prunable, meaning their folder is gone.
2. Check each one for work that exists nowhere else
For each worktree path:
git -C <path> status --short Uncommitted or untracked files.
git -C <path> log --oneline HEAD --not --remotes Commits that aren't on any remote branch. This works whether or not the branch has an upstream, and agent branches often don't.
git branch --merged main Branches already merged into main.
- Merged, or every commit pushed, and
statusis empty: safe to remove. - Unpushed commits: push the branch, or check that you don't need it.
- Uncommitted or untracked files: commit them, stash them, or copy them out first.
- Locked: an agent may still be using it. Leave it alone.
Run git fetch first, so that "merged" and "pushed" reflect the server and not your last fetch.
Don't lose your .env
A worktree is a fresh checkout, so ignored files like .env or .env.local aren't in it unless something copied them there. Claude Code does that through a .worktreeinclude file. If you edited a secret or a local config inside a worktree, that edit lives only there. git status won't show it, because the file is ignored. Check with:
git -C <path> status --short --ignored
and copy anything you need before removing the worktree.
3. Remove it the git way
git worktree remove <path>
Git only removes clean worktrees, meaning no untracked files and no changes to tracked files. That refusal is a safety net, so read it before you reach for --force. The main worktree can't be removed.
The branch stays. Delete it separately once you're sure:
git branch -d worktree-feature-auth -d refuses if the branch isn't merged.
4. Clear leftovers in .git/worktrees
If you deleted a worktree folder with rm -rf or in Finder, git still has a record of it in .git/worktrees/<name>. Git eventually removes those records on its own (see gc.worktreePruneExpire), or you can do it now:
git worktree prune -n -v Dry run: show what would be pruned.
git worktree prune -v
prune only clears records for worktrees whose folder is gone. It doesn't touch folders that still exist. A locked worktree's records aren't pruned.
What Claude Code does on exit
When you leave an interactive worktree session, Claude Code checks for changed or untracked files, uncommitted work in submodules and new commits. A clean worktree from an unnamed session is removed with its branch automatically. If there's work in it, Claude Code asks whether to keep or remove it, and removing deletes the work. Answer that prompt carefully.
Doing it with DevSweeper
DevSweeper finds worktrees from Claude Code, Codex, Cursor and others without setup, rates each one by what its branch still holds, and never removes one with uncommitted changes, commits on no branch, or a lock. It warns when an ignored file such as .env.local has no copy in the main checkout. By default it moves only the dependency and build folders to the Trash. Removing a whole worktree runs git worktree prune afterwards. The branch is always kept.
Sources (read 2 Nov 2026): git-worktree documentation; Claude Code: worktrees; Codex worktrees; Cursor worktrees.