rtk: Command for Claude Code

.claude/commands/clean-worktree.md

clean-worktree is a command for Claude Code from rtk-ai/rtk. It costs 14 tokens per session (776 once invoked), scanned C, original, Apache-2.0.

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

Written for Claude Code: installed under .claude/. Also seen: model in frontmatter; positional $N argument.

This is rtk-ai/rtk's own configuration. It tells Claude Code how to work on rtk itself, so it is not a mod to install elsewhere. Copy it as a starting point and replace the rules that are about this project. Everything rtk configures →

About the project

rtk is a command-line proxy that filters and compresses output from development commands before it reaches an LLM's context. It is used with coding agents to reduce the tokens consumed by commands such as Git operations. The catalogue includes skills, commands, agents, and instructions for using rtk in agent workflows.

rtk-ai/rtk · 84,655 stars · on GitHub · rtk-ai.app

Reuse

Borrowing it

Nothing to install: this file belongs to rtk-ai/rtk. Take a copy, put it at the same path in your own repository, and replace the rules that are about this project with yours.

Copy the file
curl -O https://raw.githubusercontent.com/rtk-ai/rtk/develop/.claude/commands/clean-worktree.md
Clone the repo
git clone --depth 1 https://github.com/rtk-ai/rtk

Made for: Claude Code.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for clean-worktree

README.md
[![agentmods](https://agentmods.dev/badge/commands/rtk-ai/rtk/clean-worktree/github.svg)](https://agentmods.dev/commands/rtk-ai/rtk/clean-worktree)
Your own site
<a href="https://agentmods.dev/commands/rtk-ai/rtk/clean-worktree"><img src="https://agentmods.dev/badge/commands/rtk-ai/rtk/clean-worktree/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for clean-worktree

Your own site · 80×15
<a href="https://agentmods.dev/commands/rtk-ai/rtk/clean-worktree"><img src="https://agentmods.dev/badge/commands/rtk-ai/rtk/clean-worktree.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
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. A grade says what 26 rules found in the file — not that it is safe.
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.1 $0.00014 $0.00776
Opus 5.5 $0.00006 $0.00310
Sonnet 5.5 $0.00003 $0.00155
Haiku 4.5 $0.00001 $0.00078

Measured 1mo ago against content hash b9f0a21dab7d, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, 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 1mo 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. 1mo 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 (84,655 stars, last pushed yesterday), licensed Apache-2.0. It adds 14 tokens to every session and 776 once invoked, about $0.0001 per session on Opus 5.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.