claudegoodies
Command

clean-worktree

From rtk-ai

Clean stale worktrees (interactive)

Install

/clean-worktree

Facts

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

Source preview

The instructions Claude Code reads when this command runs.

# Clean Worktree (Interactive)

Audit and clean obsolete worktrees interactively: merged, pruned, orphaned branches.

**vs `/tech:clean-worktrees`**:
- `/tech:clean-worktree`: Interactive, asks confirmation before deletion
- `/tech:clean-worktrees`: Automatic, no interaction (merged branches only)

## Usage

```bash
/tech:clean-worktree
```

## Implementation

```bash
#!/bin/bash

echo "=== Worktrees Status ==="
git worktree list
echo ""

echo "=== Pruning stale references ==="
git worktree prune
echo ""

echo "=== Merged branches (safe to delete) ==="
while IFS= read -r line; do
    path=$(echo "$line" | awk '{print $1}')
    branch=$(echo "$line" | grep -oE '\[.*\]' | tr -d '[]')
    [ -z "$branch" ] && continue
    [ "$branch" = "master" ] && continue
    [ "$branch" = "main" ] && continue

    if git branch --merged master | grep -q "^[* ] ${branch}$"; then
        echo "  - $branch (at $path) — MERGED"
    fi
done < <(git worktree list)
echo ""

echo "=== Clean merged worktrees? [y/N] ==="
read -r confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
    while IFS= read -r line; do
        path=$(echo "$line" | awk '{print $1}')
        branch=$(echo "$line" | grep -oE '\[.*\]' | tr -d '[]')
        [ -z "$branch" ] && continue
        [ "$branch" = "master" ] && continue
        [ "$branch" = "main" ] && continue

        if git branch --merged master | grep -q "^
View full source on GitHub →

Other slash commands