Stale worktrees are never cleaned up

Open 💬 8 comments Opened Feb 18, 2026 by atrimble-synmax

Problem

Claude Code creates git worktrees under ~/.claude-worktrees/ for parallel task execution but has no mechanism to clean them up if a session ends without proper cleanup (crash, interruption, or bug in teardown). There is also no garbage collection on subsequent session starts — each session is completely unaware of what previous sessions left behind.

This leaves behind:

  1. Full repo copies on disk (wasting space)
  2. Git branches that cannot be deleted even with git branch -D because git refuses to delete a branch checked out in any worktree
  3. Worktree refs in .git/worktrees/ that persist indefinitely

The only way a user discovers these is by stumbling into the cannot delete branch error, at which point they have to manually git worktree remove or rm -rf the directory, then git worktree prune to clean up refs.

Reproduction

  1. Use Claude Code on a project where it creates a worktree (parallel task execution)
  2. End the session (or have it crash/interrupt)
  3. Observe that ~/.claude-worktrees/<project>/<worktree-name> persists
  4. Try to delete the branch: git branch -D <worktree-name> → fails with error: cannot delete branch used by worktree

Expected behavior

  • Claude Code should clean up worktrees on session exit
  • On session start, Claude Code should scan for and prune stale worktrees from previous sessions
  • At minimum, a claude --cleanup command or similar should exist

Environment

  • macOS (Darwin 25.2.0)
  • Claude Code (CLI)
  • Found a stale worktree from January 15, 2026 that was never cleaned up across dozens of subsequent sessions

View original on GitHub ↗

This issue has 8 comments on GitHub. Read the full discussion on GitHub ↗