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.
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
- Create backup branch:
backup/dead-code-removal-{timestamp} - Run all 4 detection passes
- 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)
- Ask user to confirm removal scope
- Remove confirmed dead code, commit per category
- Run tests and build to verify nothing broke
- 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
.deadcodercor 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
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Git Commit Message Generator
Generates well-structured conventional commit messages by analyzing staged changes. Follows Conventional Commits spec with scope detection.
React Component Scaffolder
Scaffolds a complete React component with TypeScript types, Tailwind styles, Storybook stories, and unit tests. Follows project conventions automatically.
CI/CD Pipeline Generator
Generates GitHub Actions workflows for CI/CD including linting, testing, building, and deploying. Detects project stack automatically.