claudegoodies
Command

clean-worktrees

From rtk-ai

Auto-clean all stale worktrees (merged branches)

Install

/clean-worktrees

Facts

Repository
rtk-ai/rtk
Status
Actively maintained
Last commit
Model
haiku

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