| name | cleanup-worktrees |
| description | Remove merged or abandoned git worktrees |
Created: 2026-02-09-00-00
Last Updated: 2026-02-09-00-00
Cleanup Git Worktrees
Remove worktrees that are finished (PR merged) or abandoned (stale).
Usage
/cleanup-worktrees
What This Does
Step 1: Identify Cleanup Candidates
Auto-cleanup criteria:
- ✅ PR merged to main
- ✅ Branch deleted on remote
- ✅ No uncommitted changes
- ⚠️ No commits in 7+ days (ask before removing)
Keep (don't cleanup):
- ❌ PR still open
- ❌ PR in review
- ❌ Uncommitted changes
- ❌ Active work (recent commits)
Step 2: Interactive Selection
Found 3 worktrees eligible for cleanup:
1. ✅ feature-auth (PR
Path: ../worktrees/repo-feature-auth
Safe to remove
2. ✅ bugfix-login (PR
Path: ../worktrees/repo-bugfix-login
Safe to remove
3. ⚠️ feature-old (no commits in 14 days)
Path: ../worktrees/repo-feature-old
Stale - review before removing
Remove these worktrees? [Y/n/selective]
Step 3: Remove Worktrees
git -C "$WORKTREE_PATH" status --porcelain
git worktree remove "$WORKTREE_PATH"
rm -rf "$WORKTREE_PATH"
git worktree prune
git branch -d "$BRANCH_NAME"
Safety Checks
Before removing any worktree:
✅ Verify PR merged:
gh pr view $PR_NUMBER --json state,mergedAt
✅ Verify no uncommitted changes:
git status --porcelain
✅ Verify branch on remote deleted:
git ls-remote --heads origin $BRANCH_NAME
✅ Ask user confirmation:
Modes
Safe Mode (Default)
Only removes worktrees where:
- PR is merged
- Branch deleted on remote
- No uncommitted changes
- User confirms
Aggressive Mode
/cleanup-worktrees --aggressive
Also considers for removal:
- Worktrees with no commits in 7+ days
- Closed PRs (even if not merged)
- Abandoned branches
Still requires user confirmation.
Dry Run
/cleanup-worktrees --dry-run
Shows what would be removed without actually removing.
Example Session
/list-worktrees
/cleanup-worktrees
After Cleanup
Report what was removed:
✅ Cleanup Complete!
Removed:
- feature-auth (PR
- bugfix-login (PR
Kept:
- Main (main branch)
- feature-ui (PR
- feature-experimental (user choice)
Disk space freed: 150 MB
Current worktrees: 3
Run /list-worktrees to see details
Best Practices
Clean Up Regularly
After merging PR:
/cleanup-worktrees
Weekly cleanup:
/cleanup-worktrees
Before creating new worktrees:
/list-worktrees
/cleanup-worktrees
/create-worktree new-feature
Stale Worktree Detection
Worktrees with no commits in 7+ days:
Week 1: Warning (⚠️ )
Week 2: Suggest cleanup
Week 3+: Aggressive cleanup suggestion
Preserving Work
Before Cleanup: Archive Important Branches
cd ../worktrees/repo-experimental
git push origin feature-experimental
Recovering Deleted Worktree
If you need the work again:
/create-worktree feature-experimental origin/feature-experimental
git checkout feature-experimental
Automation
Auto-Cleanup After Merge
Add to git hook or GitHub Actions:
gh pr merge $PR_NUMBER && /cleanup-worktrees --auto
Scheduled Cleanup
0 9 * * 5 /cleanup-worktrees --stale --auto
Troubleshooting
Worktree Won't Remove
git worktree remove --force ../worktrees/repo-feature
cd ../worktrees/repo-feature
git add . && git commit -m "WIP"
Branch Still Exists After Cleanup
git branch -D feature-name
git push origin --delete feature-name
Related Commands
/list-worktrees - See all worktrees
/create-worktree - Create new worktree
cd - Switch to different worktree (manual cd)
Team Tip
"Don't let worktrees accumulate. Clean up merged branches immediately. Keep your workspace lean - only active work should have worktrees."
Ideal state: 3-5 active worktrees max, all with recent commits.