Skip to main content
command CX Codex

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

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/commands/clean-worktrees.md
Open on GitHub

What 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.md

Call it with /clean-worktrees. For all projects use ~/.claude/commands/

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.

Similar slash commands

All slash commands for Git and GitHub →

From the same set45

The whole set →
source-command-sync source-command-sync runs a sync check between the guide repository and its landing page, then shows which counters drifted. Handy when the site is generated from repo data. CX Codex +1 ★ 6,529repo stars guide-reviewer guide-reviewer audits technical documentation drafts for factual errors, marketing buzzwords, and AI phrasing patterns before committing. CX Codex ★ 6,529repo stars update-threat-db The update-threat-db command delegates threat database changes to AgentSec and checks the resulting feed, catalogue, and compatibility data. CX Codex +1 ★ 6,529repo stars remove-worktree remove-worktree deletes a worktree by branch name: the directory, the git entry and the local branch. Useful when merged branches leave copies behind. CX Codex +1 ★ 6,529repo stars park park stores the active session in a Markdown file so work can resume after a restart. It is made for Claude Code and Codex workflows. CX Codex +1 ★ 6,529repo stars worktree Creates a separate git worktree folder for feature or fix branches. Useful when you need to work on multiple tasks in parallel without conflicts in your main branch. CX Codex +1 ★ 6,529repo stars worktree-status worktree-status is a Claude Code command that reports the branch, path, commit hash, and modified-file count for each active git worktree. CX Codex +1 ★ 6,529repo stars track-mentions track-mentions finds fresh web references to the Claude Code Ultimate Guide and updates its tracker. Use it when you need a repeatable mention audit. CX Codex +1 ★ 6,529repo stars

Project sponsors

Companies that keep this analytics open