WorktreeRemove hook suppresses the /exit keep-or-remove picker instead of firing only on "remove"
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:
- User runs
/exit. - Claude Code shows the keep / remove prompt.
- If the user picks keep: no hook fires, worktree preserved.
- If the user picks remove:
WorktreeRemovehook fires (so the project can do its own teardown — stop containers, unbind secrets, etc.), then Claude Code performsgit 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
- In a project, add to
.claude/settings.json:
``json``
{
"hooks": {
"WorktreeRemove": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "echo 'WorktreeRemove fired' >&2"
}
]
}
]
}
}
- Launch via
claude --worktree my-feature. - Run
/exit. - 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 → removereports false success. - #36205 —
EnterWorktreeignoresWorktreeCreate/WorktreeRemovehooks.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗