clean-worktrees
From rtk-ai
Auto-clean all stale worktrees (merged branches)
Install
/clean-worktreesFacts
- Repository
- rtk-ai/rtk
- Status
- Actively maintained
- Last commit
- Model
- haiku
- Source file
- .claude/commands/clean-worktrees.md
Source preview
The instructions Claude Code reads when this command runs.
# Clean Worktrees (Automatic)
Automatically clean all stale worktrees: merged branches and orphaned git references.
**vs `/tech:clean-worktree`**:
- `/tech:clean-worktree`: Interactive, asks confirmation
- `/tech:clean-worktrees`: **Automatic**, no interaction (safe: merged only)
## Usage
```bash
/tech:clean-worktrees # Clean all merged worktrees
/tech:clean-worktrees --dry-run # Preview what would be deleted
```
## Implementation
```bash
#!/bin/bash
set -euo pipefail
DRY_RUN=false
if [[ "${ARGUMENTS:-}" == *"--dry-run"* ]]; then
DRY_RUN=true
fi
echo "๐งน Cleaning Worktrees"
echo "====================="
echo ""
# Step 1: Prune stale git references
echo "1๏ธโฃ Pruning stale git references..."
PRUNED=$(git worktree prune -v 2>&1)
if [ -n "$PRUNED" ]; then
echo "$PRUNED"
echo "โ
Stale references pruned"
else
echo "โ
No stale references found"
fi
echo ""
# Step 2: Find merged worktrees
echo "2๏ธโฃ Finding merged worktrees..."
MERGED_COUNT=0
MERGED_BRANCHES=()
while IFS= read -r line; do
path=$(echo "$line" | awk '{print $1}')
branch=$(echo "$line" | grep -oE '\[.*\]' | tr -d '[]' || true)
[ -z "$branch" ] && continue
[ "$branch" = "master" ] && continue
[ "$branch" = "main" ] && continue
[ "$path" = "$(pwd)" ] && continue
if git branch --merged master | grep -q "^[* ] ${branch}$" 2>/dev/null; then
MERGED_COUNT=$((MERGED_COUNT + 1))
View full source on GitHub โOther slash commands
feature-development
โ 229,918Workflow command scaffold for feature-development in everything-claude-code.
affaan-mupdated 14d agoMITdatabase-migration
โ 229,918Workflow command scaffold for database-migration in everything-claude-code.
affaan-mupdated 14d agoMITadd-language-rules
โ 229,918Workflow command scaffold for add-language-rules in everything-claude-code.
affaan-mupdated 14d agoMITcommit-push-pr
โ 137,934Commit, push, and open a PR
anthropicsupdated 13d agodedupe
โ 137,934Find duplicate GitHub issues
anthropicsupdated 13d agotriage-issue
โ 137,934Triage GitHub issues by analyzing and applying labels
anthropicsupdated 13d ago