agentleFS
stuartshieldsstuartshields/claude-setup/skills/clean-worktrees/SKILL.md

clean-worktrees

Skill2 starsChanged 7 months agoMITView on GitHub

Remove stale agent worktree directories and their orphaned branches. Run after parallel agent work or when .claude/worktrees/ accumulates cruft.

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

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

    line 9
  2. Distinctive:

    Run these commands and report counts:

    line 19
  3. Distinctive:

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

    line 41
  4. Very specific:

    If $ARGUMENTS contains --dry-run: report the inventory and stop.

    line 45
  5. Distinctive:

    Run in this order:

    line 49

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.