From coding-workflows
Remove worktree and branch for a completed issue; idempotent with unmerged-changes safety check
How this command is triggered — by the user, by Claude, or both
Slash command
/coding-workflows:cleanup-worktreeThis command is limited to the following tools:
The summary Claude sees in its command listing — used to decide when to auto-load this command
# Cleanup Worktree: {{issue}}
Remove the worktree and branch for a completed issue. Safe and idempotent -- warns before destructive actions, handles already-cleaned state gracefully.
**When to use:**
- After merging a PR created via `/coding-workflows:execute-issue-worktree {{issue}}`
- Cleaning up abandoned work (PR won't be merged)
- Cleaning up after a manual merge via the GitHub UI
---
## Step 0: Resolve Project Context (MANDATORY)
Read the `coding-workflows:project-context` skill and follow its protocol for resolving project context (reads workflow.yaml, auto-detects project setti...Remove the worktree and branch for a completed issue. Safe and idempotent -- warns before destructive actions, handles already-cleaned state gracefully.
When to use:
/coding-workflows:execute-issue-worktree {{issue}}Read the coding-workflows:project-context skill and follow its protocol for resolving project context (reads workflow.yaml, auto-detects project settings, validates configuration). If the skill file does not exist, STOP: "Required skill coding-workflows:project-context not found. Ensure the coding-workflows plugin is installed."
Command-specific overrides:
DO NOT GUESS configuration values. If a value cannot be read from workflow.yaml or confirmed via auto-detection, ask the user.
Parse {{issue}} to extract the issue number.
REPO_ROOT=$(git rev-parse --show-toplevel)
git worktree list
Match by:
../{repo}-{issue_num})feature/{issue_num}-*)If no match: Warn "No worktree found for issue {num}" and exit cleanly (idempotent -- not an error).
If multiple matches: List them with paths and branch names, then use AskUserQuestion with each worktree as an option (label: path, description: branch name). Use the selected worktree for subsequent steps.
For each check, warn the user and require explicit confirmation via AskUserQuestion before proceeding. Present "Proceed" and "Abort" as options, with the warning details in the description.
git -C {WORKTREE_PATH} status --porcelain
If output is non-empty: warn with the file list.
git -C {WORKTREE_PATH} log @{u}.. --oneline 2>/dev/null
If output is non-empty: warn with the commit list. (Handle case where no upstream is set.)
gh pr list --head {BRANCH} --repo {org}/{repo}
If a PR is open: warn "PR #N is still open. Deleting the branch will close the PR."
If any warnings were shown: Use AskUserQuestion with "Proceed with cleanup" and "Abort" as options, listing all warnings in the description. Do not proceed to removal without explicit user selection.
Run these steps in order:
# 1. Remove worktree
git worktree remove {WORKTREE_PATH}
# 2. Delete local branch (-d for merged, user confirms -D for unmerged)
git branch -d {BRANCH}
# 3. Prune stale worktree references
git worktree prune
If worktree is locked: Attempt git worktree unlock {path} then retry removal.
If git worktree remove fails (dirty): Use AskUserQuestion with "Force remove (--force)" and "Abort" as options, noting that uncommitted changes will be lost.
If git branch -d fails (unmerged): Use AskUserQuestion with "Force delete (-D)" and "Keep branch" as options, warning that the branch has unmerged commits.
If branch not found (already deleted): Skip gracefully and continue to prune.
List what was removed:
| Error | Action |
|---|---|
| No worktree found for issue | Warn and exit cleanly (idempotent) |
| Multiple worktrees match | List matches, use AskUserQuestion to disambiguate |
| Uncommitted changes in worktree | Warn with file list, AskUserQuestion to confirm |
| Unpushed commits on branch | Warn with commit list, AskUserQuestion to confirm |
| Open PR on branch | Warn about PR closure, AskUserQuestion to confirm |
| Worktree is locked | Attempt unlock, retry removal |
git worktree remove fails (dirty) | AskUserQuestion: "Force remove (--force)" vs "Abort" |
git branch -d fails (unmerged) | AskUserQuestion: "Force delete (-D)" vs "Keep branch" |
| Branch already deleted | Skip gracefully, continue to prune |
| Worktree was manually rm'd but still in git list | git worktree prune handles this |
/coding-workflows:merge-issue -- merges PR and cleans up in one step (preferred for normal workflow; use cleanup-worktree for abandoned work or manual merges)/coding-workflows:execute-issue-worktree -- the command that creates worktrees this command cleans upcoding-workflows:issue-workflow -- the skill defining execution phases, verification gates, and deferred work trackingnpx claudepluginhub kiwi-home/ai-coding-resources --plugin coding-workflows/prune-worktreeSafely scans and removes Git worktrees for closed GitHub issues with merged branches into the default branch, cleans local branches, and includes safety checks with user confirmations.
/worktree-cleanupCleans up Git worktree after PR completion: removes worktree and merged branch, checks PR status, warns on checklists/uncommitted changes, shows stats.
/cleanup-worktreesRemoves safe git worktrees for merged branches only (checked via git branch --merged and gh PRs), cleans orphans, categorizes kept ones, and generates a cleanup report.
/bcleanSafely cleans up local Git branches without open PRs and stale worktrees based on age threshold. Supports --dry-run, --force, --days N, --help options.
/cleanup-worktreeSafely removes a git worktree after verifying its branch is merged to main, preserving all branches and returning to the primary repo on latest main.