/refactor-clean Command
Safely identify and remove dead code with test verification at every step.Process
- Detect Dead Code - Run analysis tools
- Categorize Findings - Sort into safety tiers
- Safe Deletion Loop - Delete one item at a time, verify tests
- Handle Caution Items - Check for dynamic imports and string references
- Consolidate Duplicates - Merge near-duplicate functions
- Summary - Report results
Dead Code Detection Tools
| 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 |
Safety Tiers
| Tier | Examples | Action |
|---|---|---|
| SAFE | Unused utilities, test helpers, internal functions | Delete with confidence |
| CAUTION | Components, API routes, middleware | Verify no dynamic imports |
| DANGER | Config files, entry points, type definitions | Investigate before touching |
Safe Deletion Loop
For each SAFE item:- Run full test suite - Establish baseline (all green)
- Delete the dead code - Surgical removal
- Re-run test suite - Verify nothing broke
- If tests fail - Immediately revert and skip
- If tests pass - Move to next item
Command Syntax
Example Output
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
Related
- Commands:
/verify,/test-coverage