/refactor-clean
Safely identify and remove dead code with test verification at every step.
> /plugin marketplace add loulanyue/awesome-claude-notes > /plugin install awesome-claude-notes@awesome-claude-notes
How it fires
How this command gets triggered: by you, by Claude, or both.
- Fires itselfClaude auto-loads it when your prompt matches the work.
- You can call itInvoke it directly when you want it.
- Slash command
/refactor-clean
Context preview
What this command does when you run it.
Safely identify and remove dead code with test verification at every step.
Command definition
refactor-clean.mddescription: "Safely identify and remove dead code with test verification at every step."
Refactor Clean
Safely identify and remove dead code with test verification at every step.
Step 1: Detect Dead Code
Run analysis tools based on project type:
| Tool | What It Finds | Command | |------|--------------|---------| | knip | Unused exports, files, dependencies | `npx knip` | | depcheck | Unused npm dependencies | `npx depcheck` | | ts-prune | Unused TypeScript exports | `npx ts-prune` | | vulture | Unused Python code | `vulture src/` | | deadcode | Unused Go code | `deadcode ./...` | | cargo-udeps | Unused Rust dependencies | `cargo +nightly udeps` |
If no tool is available, use Grep to find exports with zero imports:
# Find exports, then check if they're imported anywhere
Step 2: Categorize Findings
Sort findings into safety tiers:
| Tier | Examples | Action | |------|----------|--------| | **SAFE** | Unused utilities, test helpers, internal functions | Delete with confidence | | **CAUTION** | Components, API routes, middleware | Verify no dynamic imports or external consumers | | **DANGER** | Config files, entry points, type definitions | Investigate before touching |
Step 3: Safe Deletion Loop
For each SAFE item:
1. **Run full test suite** — Establish baseline (all green) 2. **Delete the dead code** — Use Edit tool for surgical removal 3. **Re-run test suite** — Verify nothing broke 4. **If tests fail** — Immediately revert with `git checkout -- <file>` and skip this item 5. **If tests pass** — Move to next item
Step 4: Handle CAUTION Items
Before deleting CAUTION items:
- Search for dynamic imports: `import()`, `require()`, `__import__`
- Search for string references: route names, component names in configs
- Check if exported from a public package API
- Verify no external consumers (check dependents if published)
Step 5: Consolidate Duplicates
After removing dead code, look for:
- Near-duplicate functions (>80% similar) — merge into one
- Redundant type definitions — consolidate
- Wrapper functions that add no value — inline them
- Re-exports that serve no purpose — remove indirection
Step 6: Summary
Report results:
Dead Code Cleanup
──────────────────────────────
Deleted: 12 unused functions
3 unused files
5 unused dependencies
Skipped: 2 items (tests failed)
Saved: ~450 lines removed
──────────────────────────────
All tests passing ✅Rules
- **Never delete without running tests first**
- **One deletion at a time** — Atomic changes make rollback easy
- **Skip if uncertain** — Better to keep dead code than break production
- **Don't refactor while cleaning** — Separate concerns (clean first, refactor later)
Navigation
- [Command → Agent / Skill Map](../docs/COMMAND-AGENT-MAP.md)
- [Agents index](../AGENTS.md)
- [Contexts directory](../contexts)
- [Contributing guide](../CONTRIBUTING.md)
Read more
description: "Safely identify and remove dead code with test verification at every step."
Refactor Clean
Safely identify and remove dead code with test verification at every step.
Step 1: Detect Dead Code
Run analysis tools based on project type:
| Tool | What It Finds | Command | |------|--------------|---------| | knip | Unused exports, files, dependencies | `npx knip` | | depcheck | Unused npm dependencies | `npx depcheck` | | ts-prune | Unused TypeScript exports | `npx ts-prune` | | vulture | Unused Python code | `vulture src/` | | deadcode | Unused Go code | `deadcode ./...` | | cargo-udeps | Unused Rust dependencies | `cargo +nightly udeps` |
If no tool is available, use Grep to find exports with zero imports:
# Find exports, then check if they're imported anywhere
Step 2: Categorize Findings
Sort findings into safety tiers:
| Tier | Examples | Action | |------|----------|--------| | **SAFE** | Unused utilities, test helpers, internal functions | Delete with confidence | | **CAUTION** | Components, API routes, middleware | Verify no dynamic imports or external consumers | | **DANGER** | Config files, entry points, type definitions | Investigate before touching |
Step 3: Safe Deletion Loop
For each SAFE item:
1. **Run full test suite** — Establish baseline (all green) 2. **Delete the dead code** — Use Edit tool for surgical removal 3. **Re-run test suite** — Verify nothing broke 4. **If tests fail** — Immediately revert with `git checkout -- <file>` and skip this item 5. **If tests pass** — Move to next item
Step 4: Handle CAUTION Items
Before deleting CAUTION items:
- Search for dynamic imports: `import()`, `require()`, `__import__`
- Search for string references: route names, component names in configs
- Check if exported from a public package API
- Verify no external consumers (check dependents if published)
Step 5: Consolidate Duplicates
After removing dead code, look for:
- Near-duplicate functions (>80% similar) — merge into one
- Redundant type definitions — consolidate
- Wrapper functions that add no value — inline them
- Re-exports that serve no purpose — remove indirection
Step 6: Summary
Report results:
Dead Code Cleanup
──────────────────────────────
Deleted: 12 unused functions
3 unused files
5 unused dependencies
Skipped: 2 items (tests failed)
Saved: ~450 lines removed
──────────────────────────────
All tests passing ✅Rules
- **Never delete without running tests first**
- **One deletion at a time** — Atomic changes make rollback easy
- **Skip if uncertain** — Better to keep dead code than break production
- **Don't refactor while cleaning** — Separate concerns (clean first, refactor later)
Navigation
- [Command → Agent / Skill Map](../docs/COMMAND-AGENT-MAP.md)
- [Agents index](../AGENTS.md)
- [Contexts directory](../contexts)
- [Contributing guide](../CONTRIBUTING.md)
Community-maintained distribution of reusable AI coding agents, commands, skills, hooks, and cross-harness workflows.
Repo: loulanyue/awesome-claude-notes
Other commands on awesome-claude-notes.
- /aside
Answer a quick side question without interrupting or losing context from the current task. Resume work automatically after answering.
Open command - /build-fix
Incrementally fix build and type errors with minimal, safe changes.
Open command - /checkpoint
Create or verify a checkpoint in your workflow.
Open command - /claw
Start NanoClaw v2 — ECC's persistent, zero-dependency REPL with model routing, skill hot-load, branching, compaction, export, and metrics.
Open command - /code-review
Comprehensive security and quality review of uncommitted changes:
Open command - /context-budget
Analyze context window usage across agents, skills, MCP servers, and rules to find optimization opportunities. Helps reduce token overhead and avoid performance warnings.
Open command

