clean-worktrees

clean-worktrees is a skill for Claude Code from stuartshields/claude-setup. It costs 34 tokens per session (608 once invoked), scanned B, original, MIT.

A cleanup procedure for removing leftover Git worktree folders and branches created by coding agents. Git worktrees let several working copies of one repository exist at once.

In plain words
What is it for?
Use it to count stale worktrees, preview what would be removed, and clean up the folders and orphaned branches.
Why use it?
It removes unused folders and branch records that build up after parallel agent work, keeping the repository easier to inspect.

Skill for Claude Code

Written for Claude Code: argument-hint in frontmatter. Also seen: positional $N argument; mentions Claude Code; mentions Codex.

Good fit Use it to count stale worktrees, preview what would be removed, and clean up the folders and orphaned branches.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/stuartshields/claude-setup/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 stuartshields/claude-setup --skill clean-worktrees
Clone the repo
git clone --depth 1 https://github.com/stuartshields/claude-setup

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/stuartshields/claude-setup/clean-worktrees/github.svg)](https://agentmods.dev/skills/stuartshields/claude-setup/clean-worktrees)
Your own site
<a href="https://agentmods.dev/skills/stuartshields/claude-setup/clean-worktrees"><img src="https://agentmods.dev/badge/skills/stuartshields/claude-setup/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/stuartshields/claude-setup/clean-worktrees"><img src="https://agentmods.dev/badge/skills/stuartshields/claude-setup/clean-worktrees.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 34 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 608 The whole file, excluding the scripts and references it only reads on demand.
Security scan B 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.00034 $0.00608
Opus 5.5 $0.00014 $0.00243
Sonnet 5.5 $0.00007 $0.00122
Haiku 4.5 $0.00003 $0.00061

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

Security

Grade B, and why

clean-worktrees scanned grade B 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.

Reads agent configuration directoriesmediumAgent snooping

.claude/, .codex/, .gemini/ hold keys, settings and other credentials a mod has no legitimate need for.

WORKTREE_DIRS=$(find .claude/worktrees -maxdepth 1 -type d -name 'agent-*' 2>/dev/null | wc -l | tr -d ' ')
skills/clean-worktrees/SKILL.md · 73 lines

How it starts

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

Clean Stale Agent Worktrees

Remove worktree directories and orphaned worktree-agent-* branches left behind by agent isolation mode.

Based on community patterns from claude-codex-settings and claude-code-ultimate-guide, adapted for Claude Code's agent worktree pattern.

Related upstream issue: anthropics/claude-code#26725

Process

1. Inventory

Run these commands and report counts:

# Count worktree directories
WORKTREE_DIRS=$(find .claude/worktrees -maxdepth 1 -type d -name 'agent-*' 2>/dev/null | wc -l | tr -d ' ')

# Count git worktree registrations (excluding main)
GIT_WORKTREES=$(git worktree list 2>/dev/null | grep 'worktree-agent-' | wc -l | tr -d ' ')

# Count orphaned branches
ORPHANED_BRANCHES=$(git branch --list 'worktree-agent-*' 2>/dev/null | wc -l | tr -d ' ')

Report:

## Worktree Inventory

- Worktree directories: {WORKTREE_DIRS}
- Git worktree registrations: {GIT_WORKTREES}
- Orphaned branches: {ORPHANED_BRANCHES}

If all counts are 0: report "No stale worktrees found." and stop.

2. Dry Run Check

If $ARGUMENTS contains --dry-run: report the inventory and stop. Do not delete anything.

3. Clean Up

Run in this order:

# Prune stale git worktree references
git worktree prune

# Remove registered worktrees that match agent pattern
git worktree list | grep 'worktree-agent-' | awk '{print $1}' | while read wt; do
  git worktree remove --force "$wt"
done

# Delete orphaned branches (allowed by block-git-commit.sh for worktree-agent-* only)
git branch --list 'worktree-agent-*' | xargs git branch -D

4. Report

## Cleanup Complete

- Removed {N} worktree directories
- Deleted {N} orphaned branches
- Pruned git worktree references

Read the full file on GitHub · 73 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 · 73 lines · 34 tokens per session scan B a9b09ec37532

Subscribe to this mod's changes

clean-worktrees is a skill published in the GitHub repository stuartshields/claude-setup (2 stars, last pushed 5mo ago), licensed MIT. It adds 34 tokens to every session and 608 once invoked, about $0.0001 per session on Opus 5.5. A static security scan graded it B with 1 finding (reads agent configuration directories). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

commit

Commit changes with well-crafted messages, grouping related files into separate commits.

Weaverse/.agents · 13 tokens

commit

Use this skill when the user wants to create a git commit, stage files, write a commit message, or finalize their work with a git commit. Also invoked by phrases like "commit my changes", "save to git", "git commit".

Imran-ml/claude-skills · 51 tokens

review-pr

Use this skill when the user wants to review a pull request, check a PR for issues, or get feedback on a GitHub PR. Triggered by "review PR", "check PR", "review pull request", or "/review-pr ".

Imran-ml/claude-skills · 56 tokens

push-to-forked-pr

Push the current working tree directly to a GitHub PR whose head lives on a fork, without creating a new branch and without pushing to origin (which is usually the upstream). Invoke when the user says things like "push straight to PR.

hoangsonww/Claude-Code-Agent-Monitor · 57 tokens

version-release

Choose and apply the correct semantic version bump for this repository. Use for every user-visible release, before merge when a change set should ship as patch, minor, or major, and whenever package/plugin/desktop version metadata must stay synchronized.

hoangsonww/Claude-Code-Agent-Monitor · 50 tokens

history-portability

Import Claude Code or Codex history and move complete CCAM datasets between machines. Use when rescanning provider history, importing a copied directory, uploading JSONL or archives, exporting a backup, restoring it idempotently, or verifying that tokens, workflows, runs, rules, and pricing survived.

hoangsonww/Claude-Code-Agent-Monitor · 63 tokens