R

Remove Dead Code Command

Safely identifies and removes dead code using multi-pass scanning with automatic backup branches. Detects unused exports, unreachable functions, orphaned files, and stale feature flags across your entire codebase.

CommandCommunitycode reviewv1.0.0MIT
0 views0 copies

Command

/remove-dead-code

Description

Performs comprehensive dead code detection and safe removal across your codebase. Uses multi-pass analysis to find unused exports, unreachable code paths, orphaned files, and stale feature flags — then removes them with full git backup.

Behavior

Pass 1: Unused Exports Detection

Scan all exported functions, classes, constants, and types. Cross-reference against all import statements across the project.

// DETECTED: exported but never imported anywhere export function formatLegacyDate(date: string): string { ... } export const OLD_API_BASE = "https://api-v1.example.com"; export interface DeprecatedUser { ... }

Pass 2: Unreachable Code Paths

Identify code after early returns, inside impossible conditions, or behind permanently-false feature flags.

function process(data: Data) { if (!data) return null; // ... main logic ... return result; // DETECTED: unreachable code below console.log("debug"); cleanup(); }

Pass 3: Orphaned Files

Find files with no inbound imports — not referenced by any other file, route config, or build entry point.

Pass 4: Stale Dependencies

Check package.json dependencies against actual import usage.

Workflow

  1. Create backup branch: backup/dead-code-removal-{timestamp}
  2. Run all 4 detection passes
  3. Present findings grouped by confidence level:
    • High confidence — Safe to remove (unused local functions, unreachable code)
    • Medium confidence — Likely dead (unused exports in internal modules)
    • Low confidence — Needs review (potentially used via dynamic imports, reflection)
  4. Ask user to confirm removal scope
  5. Remove confirmed dead code, commit per category
  6. Run tests and build to verify nothing broke
  7. If verification fails, identify which removal caused it and revert that specific change

Output Format

## Dead Code Report ### High Confidence (safe to remove) - `src/utils/legacy.ts` — 3 unused exports (formatLegacyDate, OLD_API_BASE, DeprecatedUser) - `src/components/OldModal.tsx` — orphaned file, 0 imports - `src/services/v1Api.ts` — entire file unreferenced ### Medium Confidence (likely dead) - `src/helpers/index.ts` — export `debounceV1` unused (only `debounce` is imported) ### Low Confidence (review needed) - `src/plugins/analytics.ts` — no static imports but may be loaded dynamically ### Summary | Category | Files | Lines | |----------|-------|-------| | Unused exports | 12 | 340 | | Unreachable code | 5 | 87 | | Orphaned files | 3 | 420 | | Stale deps | 2 | — | | **Total** | **22** | **847** |

Rules

  • ALWAYS create a backup branch before any removal
  • NEVER remove code without presenting findings first
  • Skip node_modules, dist, .git, and build output directories
  • Respect .deadcoderc or similar config files if present
  • Do not remove test files even if they appear orphaned
  • Do not remove files matching common entry point patterns (index., main., app.*)
  • If tests fail after removal, revert and flag for manual review

Examples

# Full scan and removal /remove-dead-code # Scan only, no removal /remove-dead-code --scan-only # Target specific directory /remove-dead-code src/services
Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates