claude-code - 💡(How to fix) Fix WorktreeRemove hook suppresses the /exit keep-or-remove picker instead of firing only on "remove"

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

When a project registers a WorktreeRemove hook in .claude/settings.json, Claude Code replaces the entire /exit keep-or-remove picker with the hook invocation instead of only firing the hook when the user explicitly selects remove. This removes the user's ability to choose "keep" and forces teardown on every exit.

Root Cause

Isolated-environment workflows need a teardown handler on the "remove" path:

  • Per-worktree Supabase containers must be stopped.
  • Per-worktree Doppler bindings must be unbound.
  • Tunnels, ports, and other side resources need cleanup.

Today the only way to register that teardown is to register WorktreeRemove, which breaks the keep/remove UX. Teams resort to:

  • Not registering the hook and accepting orphaned resources.
  • Registering the hook and accepting that "keep" no longer works.
  • Defending against spurious fires with custom gates inside the hook (we have done this — see worktree-teardown.sh gate comments) — which is brittle and a maintenance burden.

Code Example

{
     "hooks": {
       "WorktreeRemove": [
         {
           "matcher": "*",
           "hooks": [
             {
               "type": "command",
               "command": "echo 'WorktreeRemove fired' >&2"
             }
           ]
         }
       ]
     }
   }
RAW_BUFFERClick to expand / collapse

Summary

When a project registers a WorktreeRemove hook in .claude/settings.json, Claude Code replaces the entire /exit keep-or-remove picker with the hook invocation instead of only firing the hook when the user explicitly selects remove. This removes the user's ability to choose "keep" and forces teardown on every exit.

Expected

The WorktreeRemove hook should fire only when the user picks "remove" from the standard /exit picker:

  1. User runs /exit.
  2. Claude Code shows the keep / remove prompt.
  3. If the user picks keep: no hook fires, worktree preserved.
  4. If the user picks remove: WorktreeRemove hook fires (so the project can do its own teardown — stop containers, unbind secrets, etc.), then Claude Code performs git worktree remove.

Actual

Registering a WorktreeRemove hook suppresses the keep/remove picker entirely. The hook fires unconditionally on session exit. There is no way to register a teardown handler without also losing the user's choice.

Why this matters

Isolated-environment workflows need a teardown handler on the "remove" path:

  • Per-worktree Supabase containers must be stopped.
  • Per-worktree Doppler bindings must be unbound.
  • Tunnels, ports, and other side resources need cleanup.

Today the only way to register that teardown is to register WorktreeRemove, which breaks the keep/remove UX. Teams resort to:

  • Not registering the hook and accepting orphaned resources.
  • Registering the hook and accepting that "keep" no longer works.
  • Defending against spurious fires with custom gates inside the hook (we have done this — see worktree-teardown.sh gate comments) — which is brittle and a maintenance burden.

Reproduction

  1. In a project, add to .claude/settings.json:
    {
      "hooks": {
        "WorktreeRemove": [
          {
            "matcher": "*",
            "hooks": [
              {
                "type": "command",
                "command": "echo 'WorktreeRemove fired' >&2"
              }
            ]
          }
        ]
      }
    }
  2. Launch via claude --worktree my-feature.
  3. Run /exit.
  4. Observe: no keep/remove prompt appears. The hook runs immediately.

Suggested fix

Keep the /exit keep/remove picker active when a WorktreeRemove hook is registered. Fire the hook only on the "remove" branch, before git worktree remove. Pass the same payload Claude Code internally uses (worktree path, branch name) so the hook can run targeted teardown.

Bonus: also fire WorktreeRemove when the worktree is removed automatically due to no-changes-on-exit (per the docs: "No changes: the worktree and its branch are removed automatically"). Today there is no hook surface for that path either.

Related

  • #58431 — claude --worktree <existing-name> does not register worktree; /exit → remove reports false success.
  • #36205 — EnterWorktree ignores WorktreeCreate/WorktreeRemove hooks.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix WorktreeRemove hook suppresses the /exit keep-or-remove picker instead of firing only on "remove"