How to Clean Up Worktrees Claude Code, Cursor Leave Behind

AI coding agents are transforming software development workflows, but they come with an unexpected side effect: filesystem clutter. Tools like Claude Code, Cursor and Codex create temporary Git worktrees during code generation and testing, and these worktrees often persist long after tasks complete. As developers adopt these agents for daily coding tasks, understanding how to manage and clean up orphaned worktrees has become an essential maintenance skill.
Officially released on November 16, 2026, a new technical guide addresses this growing pain point for developers working with AI coding assistants across multiple projects.
Why AI Coding Agents Create Worktrees
Git worktrees allow multiple working directories to coexist from a single repository, enabling parallel development without branch switching overhead. AI coding agents leverage this feature to test code changes, run experiments, and validate modifications in isolated environments. When an agent completes a task, it typically returns control to the developer but may leave the worktree behind. Over weeks or months, dozens of these orphaned worktrees can accumulate, each consuming disk space and cluttering your repository metadata.
The problem intensifies for teams running agents on large codebases or using multiple AI assistants simultaneously. Each agent session may spawn new worktrees without cleaning up previous ones, creating a maintenance burden that slows down repository operations and confuses version control workflows.
Identifying Orphaned Worktrees
Before removing worktrees, you need to identify which ones are safe to delete. Run git worktree list to see all worktrees associated with your repository. This command displays the path, branch, and commit hash for each worktree. Look for entries pointing to temporary directories or branches created by coding agents, often named with timestamps or random identifiers.
- Check for worktrees in temporary directories like
/tmpor agent-specific cache folders - Identify worktrees with no recent file modifications using filesystem timestamps
- Cross-reference active agent sessions to confirm which worktrees are truly orphaned
Running git worktree prune removes references to worktrees that no longer exist on disk, but it won't delete the actual directories. For complete cleanup, you need to remove both the worktree registration and the filesystem directories.
Safe Removal Commands
To remove a specific worktree, use git worktree remove <path> which handles both the Git metadata and directory deletion. If the worktree contains uncommitted changes, Git will warn you and require the --force flag to proceed. For bulk cleanup, combine Unix tools with Git commands to automate the process across multiple worktrees.
A typical cleanup workflow involves listing all worktrees, filtering for agent-created paths, verifying no active processes are using them, then removing each systematically. Shell scripts can automate this process and integrate with cron jobs for regular maintenance. The referenced guide provides complete command examples and automation scripts tailored to different operating systems and shell environments.
Preventing Future Accumulation
Rather than relying solely on manual cleanup, configure your AI coding agents to handle worktree lifecycle management automatically. Many agents support cleanup hooks or configuration options to remove worktrees after task completion. Review your agent settings and enable automatic cleanup where available.
For agents without built-in cleanup, wrapper scripts can intercept agent execution and ensure worktree removal on exit. Implementing a weekly automated cleanup job provides a safety net that catches orphaned worktrees before they accumulate into a significant problem. Document your cleanup procedures in team wikis so all developers maintain consistent practices.
What This Means for Developers
As AI coding agents become standard development tools, understanding their filesystem impact is crucial for maintaining clean, efficient repositories. Worktree cleanup may seem like a minor maintenance task, but neglecting it leads to degraded repository performance, confused version control state, and wasted disk space. Developers should treat worktree management as part of routine repository hygiene, alongside branch cleanup and dependency updates. The emergence of specialized guides for AI agent maintenance signals the maturation of this technology category and the need for new operational best practices in AI-assisted development workflows.
on Emergent today





