{"slug": "how-to-clean-up-the-worktrees-claude-code-cursor-and-codex-leave-behind", "title": "How to clean up the worktrees Claude Code, Cursor and Codex leave behind", "summary": "Coding agents Claude Code, Cursor and Codex each leave behind git worktrees that accumulate to tens of gigabytes, according to a Mopup guide that cites measured cases including 61 Codex worktrees taking about 100 GB on a Rails app (Codex issue 35383) and a Claude Code user finding 27 forgotten worktrees using 80 GB (Claude Code issue 63087). Each tool cleans up only its own worktrees by count or age — Cursor keeps the newest 25, Codex keeps the 15 most recent, and Claude Code sweeps subagent and background-session worktrees after 30 days — so the guide recommends listing them with `git worktree list` and sizing them with `du -sh` before deleting. The guide warns that automatic cleanups have removed wanted work, including a git-ignored images folder (issue 98186) and unpushed submodule changes (issue 107997).", "body_md": "[Mopup](https://mopup.online/) › [Guides](https://mopup.online/guides) › Coding agent worktrees\n\n# How to clean up the worktrees Claude Code, Cursor and Codex leave behind\n\nCoding agents work in git worktrees so several can run at once. Each worktree is a full checkout with its own node_modules and build output, and each tool cleans up only its own, by count or by age. They add up quietly, often to tens of gigabytes.\n\n## How much space they take\n\nA worktree costs what a fresh clone costs once its dependencies are installed. Some measured examples:\n\n- A Laravel project: about 530 MB of node_modules and 350 MB of vendor per worktree, so ten worktrees took 9.9 GB\n          ([Mattias Geniar](https://ma.ttias.be/deduplicating-git-worktrees-ext4-jdupes) ).\n- A Rails app with about 1.3 GB of node_modules per checkout: 61 Codex worktrees took about 100 GB\n          ([Codex issue 35383](https://github.com/openai/codex/issues/35383) ).\n- A Claude Code user found 27 forgotten worktrees using 80 GB\n          ([Claude Code issue 63087](https://github.com/anthropics/claude-code/issues/63087) ).\n\n## Where each agent keeps them\n\n| Tool | Where its worktrees are | What it cleans up by itself | \n|---|---|---|\n| Claude Code | `.claude/worktrees/<name>` inside your repository, on a branch called`worktree-<name>` | When you exit, it removes a clean worktree from an unnamed session, and asks about named ones or ones with changes. Worktrees from subagents and background sessions are swept after 30 days unless they hold work. | \n| Claude Code desktop app | `.claude/worktrees` inside your project, unless you change Worktree location in its settings | Removes a leftover worktree about 30 days after its session is archived or last opened. It can also archive sessions once their pull request merges. | \n| Cursor | `~/.cursor/worktrees/<repository>/<name>` | Keeps the newest 25 on your Mac by default and deletes older ones. Archiving an agent deletes its worktree about 30 seconds later. | \n| Codex | `~/.codex/worktrees` , on a detached HEAD rather than a branch | Keeps your 15 most recent Codex worktrees, and deletes one when you archive its chat, after taking a snapshot. | \n\nSources: Anthropic's docs on [worktrees](https://code.claude.com/docs/en/worktrees) and the\n        [desktop app](https://code.claude.com/docs/en/desktop), Cursor's\n        [worktree settings](https://cursor.com/docs/configuration/worktrees), and OpenAI's\n        [Codex worktrees](https://learn.chatgpt.com/docs/environments/git-worktrees) page. Details change between\n        versions. Bug reports say older Claude Code versions used `~/.claude-worktrees`, so look there too.\n\nThe automatic cleanups don't catch everything, and they have deleted work people wanted: a git-ignored folder of\n        images ([issue 98186](https://github.com/anthropics/claude-code/issues/98186)) and submodule changes nobody\n        had pushed ([issue 107997](https://github.com/anthropics/claude-code/issues/107997)). Cursor's documentation\n        doesn't promise to keep worktrees with uncommitted changes, so commit or stash before you archive an agent.\n\n## See every worktree and its size\n\nFrom inside a repository, list its worktrees, wherever the agent put them:\n\n```\ngit worktree list\n```\n\nTo see how big they are, without changing anything:\n\n```\ndu -sh .claude/worktrees/* 2>/dev/null | sort -h\ndu -sh ~/.cursor/worktrees/*/* 2>/dev/null | sort -h\ndu -sh ~/.codex/worktrees/* 2>/dev/null | sort -h\n```\n\nRun the first line inside each repository you use Claude Code in. The other two cover every repository at once.\n\n## Check a worktree is finished\n\nIn these commands, `<path>` is the worktree's folder and `<branch>` its branch. Use\n        your repository's main branch name if it isn't `main`.\n\n**Nothing uncommitted.** This prints nothing when the worktree is clean:\n\n```\ngit -C <path> status --short\n```\n\n**Nothing you need among the ignored files.** Removing a worktree also deletes files git ignores, such as\n        `.env`, `node_modules` and build output, without asking. List them first, and copy anything you\n        want to keep:\n\n```\ngit -C <path> status --short --ignored\n```\n\n**The branch is merged.** This prints nothing when every commit on the branch is already in main:\n\n```\ngit log --oneline main..<branch>\n```\n\nSquash merges on GitHub leave the branch's commits out of main, so the command above still lists them. Ask GitHub\n        instead, with the `gh` command:\n\n```\ngh pr list --head <branch> --state merged --json number,headRefOid\ngit rev-parse <branch>\n```\n\nIf a merged pull request's `headRefOid` matches the second line, everything on the branch went into that\n        pull request.\n\n**For Codex worktrees,** which aren't on a branch, check that no commit exists only there. This prints\n        nothing when every commit is also on a branch or a remote:\n\n```\ngit -C <path> log --oneline HEAD --not --branches --remotes\n```\n\n## Remove it\n\nClose the agent session that uses the worktree first. Then, from the repository:\n\n```\ngit worktree remove <path>\n```\n\nGit refuses if the worktree has uncommitted changes or untracked files, which is what you want. The branch stays, so\n        nothing committed is lost. Once you no longer need the branch, delete it with `git branch -d <branch>`,\n        which refuses if the branch isn't merged. After a squash merge git still sees it as unmerged; once `gh` has\n        confirmed the merge, `git branch -D <branch>` deletes it.\n\n- **\"Locked\" error:** runs such as`claude -p` can leave a worktree locked. If no session is using\n          it, unlock it with`git worktree unlock <path>` and remove it again.\n- **Avoid `--force`:** it removes the worktree even with uncommitted work in it. Use it only after\n          the checks above.\n- **Deleted the folder by hand?** Git still lists it.`git worktree prune` clears those entries,\n          and`git worktree prune -n` shows what it would clear.\n\n## Codex crash reports filling up?\n\nSome versions of the Codex app saved crash reports so fast that one user's folder grew by 4.9 GB and 54,504 files in a\n        day ([Codex issue 25921](https://github.com/openai/codex/issues/25921)). OpenAI says a fix has been merged.\n        To check, run:\n\n```\ndu -sh ~/Library/Application\\ Support/Codex/Crashpad/pending \\\n  ~/Library/Application\\ Support/com.openai.codex/web/Crashpad/pending 2>/dev/null\n```\n\nIf it's large, quit Codex, open the folder in Finder with Go › Go to Folder, and move the files inside\n        `pending` to the Trash. People who reported the bug freed the space this way.\n\n## Keep them from piling up\n\n- **Claude Code:** set`cleanupPeriodDays` lower than its default of 30 to sweep finished subagent\n          and background worktrees sooner. It never sweeps ones that still hold work. In the desktop app, turn on Auto-archive\n          after PR merge or close.\n- **Cursor:** lower`cursor.worktreeMaxCount` , which is 25 by default, and commit or stash before\n          archiving agents.\n- **Codex:** lower the limit of 15 kept worktrees in the app's settings.\n- **Share dependencies:** with pnpm,`virtualStoreType: global` lets worktrees share one package\n          store instead of a full node_modules each ([pnpm's guide](https://pnpm.io/git-worktrees) ).\n\n## Related guides\n\n- [**How to find old coding projects taking up space** Find the projects you've stopped working on, see what their dependencies and build folders hold, and clear them safely.](https://mopup.online/guides/old-projects-disk-space)\n- [**How to find and delete old node_modules** Find every node_modules folder and its size, remove the ones you don't need, and clear package caches.](https://mopup.online/guides/node-modules-disk-space)\n- [**How to free up Docker Desktop disk space** See what fills Docker's disk image and reclaim space with Docker's own prune commands.](https://mopup.online/guides/docker-disk-space-mac)", "url": "https://wpnews.pro/news/how-to-clean-up-the-worktrees-claude-code-cursor-and-codex-leave-behind", "canonical_source": "https://mopup.online/guides/coding-agent-worktrees", "published_at": "2026-11-16 09:15:31+00:00", "updated_at": "2026-11-16 11:24:45.874817+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools"], "entities": ["Claude Code", "Cursor", "Codex", "Anthropic", "OpenAI", "Mopup", "Mattias Geniar"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/how-to-clean-up-the-worktrees-claude-code-cursor-and-codex-leave-behind", "markdown": "https://wpnews.pro/news/how-to-clean-up-the-worktrees-claude-code-cursor-and-codex-leave-behind.md", "text": "https://wpnews.pro/news/how-to-clean-up-the-worktrees-claude-code-cursor-and-codex-leave-behind.txt", "jsonld": "https://wpnews.pro/news/how-to-clean-up-the-worktrees-claude-code-cursor-and-codex-leave-behind.jsonld"}}