clean-worktrees

clean-worktrees is a skill for Claude Code from brain-bootstrap/claude-code-brain-bootstrap. It costs 31 tokens per session (882 once invoked), scanned C, original, MIT.

A cleanup helper for Git worktrees, which are extra working folders linked to branches. It removes worktrees whose branches have already been merged into the main branch.

In plain words
What is it for?
Use it after merging pull requests or during regular maintenance to preview and remove worktrees for merged branches.
Why use it?
Old worktrees can clutter your computer and make it harder to see which working folders are still needed. A dry-run option lets you preview the cleanup first.

Skill for Claude Code

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

Good fit Use it after merging pull requests or during regular maintenance to preview and remove worktrees for merged branches.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/brain-bootstrap/claude-code-brain-bootstrap/clean-worktrees
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.

Any agent
npx skills add brain-bootstrap/claude-code-brain-bootstrap --skill clean-worktrees
Clone the repo
git clone --depth 1 https://github.com/brain-bootstrap/claude-code-brain-bootstrap

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-worktrees

README.md
[![agentmods](https://agentmods.dev/badge/skills/brain-bootstrap/claude-code-brain-bootstrap/clean-worktrees/github.svg)](https://agentmods.dev/skills/brain-bootstrap/claude-code-brain-bootstrap/clean-worktrees)
Your own site
<a href="https://agentmods.dev/skills/brain-bootstrap/claude-code-brain-bootstrap/clean-worktrees"><img src="https://agentmods.dev/badge/skills/brain-bootstrap/claude-code-brain-bootstrap/clean-worktrees/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-worktrees

Your own site · 80×15
<a href="https://agentmods.dev/skills/brain-bootstrap/claude-code-brain-bootstrap/clean-worktrees"><img src="https://agentmods.dev/badge/skills/brain-bootstrap/claude-code-brain-bootstrap/clean-worktrees.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 31 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 882 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.00031 $0.00882
Opus 5.5 $0.00012 $0.00353
Sonnet 5.5 $0.00006 $0.00176
Haiku 4.5 $0.00003 $0.00088

Measured 1mo ago against content hash 002788840463, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade C, and why

clean-worktrees 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" 2>/dev/null; git worktree prune 2>/dev/null; }
.claude/skills/clean-worktrees/SKILL.md · 111 lines

How it starts

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

Clean Worktrees Skill

Automatically remove all worktrees for branches merged into the default branch (main or master). No interaction required.

Usage

/clean-worktrees              # Remove all merged worktrees
/clean-worktrees --dry-run    # Preview what would be deleted

Implementation

Execute this script:

#!/bin/bash
set -euo pipefail

DRY_RUN=false
if [[ "${ARGUMENTS:-}" == *"--dry-run"* ]]; then
  DRY_RUN=true
fi

DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's|.*/||' || \
                 git branch --list main master 2>/dev/null | head -1 | xargs || \
                 echo "main")

echo "Cleaning Worktrees"
echo "=================="
echo "Default branch: $DEFAULT_BRANCH"
echo ""

echo "1. Pruning stale git references..."
PRUNED=$(git worktree prune -v 2>&1)
[ -n "$PRUNED" ] && echo "$PRUNED" || echo "No stale references found"
echo ""

echo "2. Finding merged worktrees..."
MERGED_COUNT=0
MERGED_BRANCHES=()
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 "$DEFAULT_BRANCH" 2>/dev/null | grep -q "^[* ] ${branch}$"; then
    MERGED_COUNT=$((MERGED_COUNT + 1))
    MERGED_BRANCHES+=("$branch|$path")
    echo "  - $branch (merged)"
  fi
done < <(git worktree list)

if [ $MERGED_COUNT -eq 0 ]; then
  echo "No merged worktrees found"
  git worktree list
  exit 0
fi

echo "Found $MERGED_COUNT merged worktree(s)"

if [ "$DRY_RUN" = true ]; then
  echo "DRY RUN — Would delete:"
  for item in "${MERGED_BRANCHES[@]}"; do
    branch=$(echo "$item" | cut -d'|' -f1)
    path=$(echo "$item" | cut -d'|' -f2)
    echo "  - $branch ($path)"
  done
  echo "Run without --dry-run to actually delete"
  exit 0
fi

echo "3. Removing merged worktrees..."
REMOVED_COUNT=0
for item in "${MERGED_BRANCHES[@]}"; do
  branch=$(echo "$item" | cut -d'|' -f1)
  path=$(echo "$item" | cut -d'|' -f2)
  echo "Removing: $branch"
  git worktree remove "$path" 2>/dev/null || { rm -rf "$path" 2>/dev/null; git worktree prune 2>/dev/null; }
  git branch -d "$branch" 2>/dev/null || true
  REMOVED_COUNT=$((REMOVED_COUNT + 1))
done

echo ""
echo "Removed: $REMOVED_COUNT worktree(s)"
git worktree list

Read the full file on GitHub · 111 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 · 111 lines · 31 tokens per session scan C 002788840463

Subscribe to this mod's changes

clean-worktrees is a skill published in the GitHub repository brain-bootstrap/claude-code-brain-bootstrap (11 stars, last pushed 5mo ago), licensed MIT. It adds 31 tokens to every session and 882 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.