clean-worktree

An interactive Git cleanup command for checking worktrees, which are separate working folders connected to one repository. It finds stale references and branches that have already been merged, then asks before deleting them.

In plain words
What is it for?
Use it to inspect Git worktrees, remove stale references, and clean up merged branches after development work is finished.
Why use it?
It helps reduce clutter from old working folders and finished branches while keeping the cleanup under your control. It also avoids deleting the current folder or the main branches.

Command for Claude Code

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/rtk-ai/rtk/clean-worktree
Clone the repo
git clone --depth 1 https://github.com/rtk-ai/rtk

Made for: Claude Code.

Per session 14 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 776 The whole file, excluding the scripts and references it only reads on demand.
Security scan C 1 finding. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00014 $0.00776
Opus 5 $0.00007 $0.00388
Sonnet 5 $0.00003 $0.00155
Haiku 4.5 $0.00001 $0.00078

Measured 2d ago against content hash b9f0a21dab7d, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade C, and why

clean-worktree scanned grade C with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Recursive force deletehighDestructive command

rm -rf with a variable or a broad path is one typo away from removing the wrong tree.

git worktree remove "$path" 2>/dev/null || rm -rf "$path"
.claude/commands/clean-worktree.md · 106 lines

How it starts

The opening of the file, as written. The whole thing — 106 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Clean Worktree (Interactive)

Interactive cleanup of worktrees: lists merged/stale branches and asks confirmation before deleting.

Difference with /clean-worktrees:

  • /clean-worktree: Interactive, asks confirmation
  • /clean-worktrees: Automatic, no interaction

Usage

/clean-worktree    # Interactive audit + cleanup

Implementation

Execute this script:

#!/bin/bash
set -euo pipefail

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

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

echo "=== Merged branches (safe to delete) ==="
MERGED_FOUND=false
CURRENT_DIR="$(pwd)"

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" = "$CURRENT_DIR" ] && continue

  if git branch --merged master | grep -q "^[* ] ${branch}$" 2>/dev/null; then
    echo "  - $branch (at $path) - MERGED"
    MERGED_FOUND=true
  fi
done < <(git worktree list)

if [ "$MERGED_FOUND" = false ]; then
  echo "  (none found)"
  echo ""
  echo "=== Disk usage ==="
  du -sh .worktrees/ 2>/dev/null || echo "No .worktrees directory"
  exit 0
fi
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 '[]' || true)
    [ -z "$branch" ] && continue
    [ "$branch" = "master" ] && continue
    [ "$branch" = "main" ] && continue
    [ "$path" = "$CURRENT_DIR" ] && continue

    if git branch --merged master | grep -q "^[* ] ${branch}$" 2>/dev/null; then
      echo "  Removing $branch..."
      git worktree remove "$path" 2>/dev/null || rm -rf "$path"
      git branch -d "$branch" 2>/dev/null || echo "    (branch already deleted)"
      echo "  Done: $branch"
    fi
  done < <(git worktree list)
  echo ""
  echo "Cleanup complete."
else
  echo "Aborted."
fi

echo ""
echo "=== Disk usage ==="
du -sh .worktrees/ 2>/dev/null || echo "No .worktrees directory"

Read the full file on GitHub · 106 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 106 lines · 14 tokens per session scan C b9f0a21dab7d

Subscribe to this mod's changes

clean-worktree is a command published in the GitHub repository rtk-ai/rtk (78,131 stars, last pushed today), licensed Apache-2.0. It adds 14 tokens to every session and 776 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it C with 1 finding (recursive force delete). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.