# clean-worktrees

clean-worktrees cleans up Git worktrees for branches already merged into main. Useful after landing pull requests to reclaim disk space without prompt fatigue.

- command · `type=command` · Claude Code, Codex, GitHub Copilot, Gemini CLI · ★ 6529 repo stars
- Author: FlorianBruniaux
- License: CC-BY-SA-4.0 · policy=link-only · description only; the file stays with the author
- Source: https://github.com/FlorianBruniaux/claude-code-ultimate-guide/tree/HEAD/.claude/commands/clean-worktrees.md
- Updated: 2026-10-22
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/commands/clean-worktrees

## What it does

This command runs a Bash script that begins by pruning stale references with git worktree prune. Next, it iterates over all active worktrees and checks which branches are fully merged into main.

Safety safeguards prevent unwanted deletions: the script explicitly ignores main and develop branches, alongside the primary repository root. Eligible worktree directories get unlinked, followed by local branch deletion via git branch -d.

Passing --dry-run prints every candidate path without touching disk contents. Without this flag, cleanup proceeds unattended.

## Install

### Claude Code (native platform)

**Manually: put the files in place**
- File: `.claude/commands/clean-worktrees.md` — Call it with /clean-worktrees. For all projects use ~/.claude/commands/

### Codex

**Manually: put the files in place**
- File: `~/.codex/prompts/clean-worktrees.md` — Call it with /prompts:clean-worktrees. Codex marks this format deprecated in favour of skills

### GitHub Copilot

**Manually: put the files in place**
- File: `.github/prompts/clean-worktrees.prompt.md`

### Gemini CLI

**Manually: put the files in place**
- File: `.gemini/commands/clean-worktrees.toml` — Gemini stores commands as TOML: the prompt text goes into the prompt field

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/FlorianBruniaux/claude-code-ultimate-guide/tree/HEAD/.claude/commands/clean-worktrees.md

## When to use it
- purge merged Git worktrees across local checkouts
- preview candidates for deletion via a dry-run flag
- delete abandoned local branches alongside their working trees
- prune stale git worktree metadata references

## Example prompts
- /clean-worktrees
- /clean-worktrees --dry-run
- Run /clean-worktrees to prune merged branches

## Similar
- [git-worktrees](https://affpapa.org/en/nevibecode/skills/git-worktrees.md) — skill · 1200 installs
- [list_worktrees_prompt](https://affpapa.org/en/nevibecode/commands/list-worktrees-prompt.md) — command · ★ 1573 repo stars
- [create-worktrees](https://affpapa.org/en/nevibecode/plugins/create-worktrees.md) — plugin · ★ 2858 repo stars
- [using-git-worktrees](https://affpapa.org/en/nevibecode/skills/julianobarbosa-using-git-worktrees.md) — skill · 106 installs
- [git-pushing](https://affpapa.org/en/nevibecode/skills/sickn33-git-pushing.md) — skill · 932 installs
- [agent-rules](https://affpapa.org/en/nevibecode/skills/agent-rules.md) — skill · 850 installs

From the same set: [florianbruniaux/claude-code-ultimate-guide](https://affpapa.org/en/nevibecode/by/florianbruniaux/claude-code-ultimate-guide.md) — 45 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
