clean-worktrees — Claude Code command: what it does, how to install
clean-worktrees cleans up Git worktrees for branches already merged into main. Useful after landing pull requests to reclaim disk space without prompt fatigue.
CC-BY-SA-4.0 ★ 6,529 updated 2026-10-22
.claude/commands/clean-worktrees.mdWhat clean-worktrees 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.
What it helps with
- 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 for the agent
- /clean-worktrees
- /clean-worktrees --dry-run
- Run /clean-worktrees to prune merged branches
How to install clean-worktrees
Manually: put the files in place
.claude/commands/clean-worktrees.mdCall it with /clean-worktrees. For all projects use ~/.claude/commands/
Manually: put the files in place
~/.codex/prompts/clean-worktrees.mdCall it with /prompts:clean-worktrees. Codex marks this format deprecated in favour of skills
Manually: put the files in place
.github/prompts/clean-worktrees.prompt.mdManually: put the files in place
.gemini/commands/clean-worktrees.tomlGemini stores commands as TOML: the prompt text goes into the prompt field
Questions about clean-worktrees
How to install clean-worktrees in Claude Code?
Place the command markdown file at .claude/commands/clean-worktrees.md in your project root.
What does clean-worktrees do?
It identifies worktrees whose branches are merged into main, deletes their directories, and removes the local branch references.
Does clean-worktrees touch active or unmerged worktrees?
No. The underlying routine hardcodes exclusions for main, develop, and the primary repo directory, skipping unmerged trees entirely.