Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Refactoringbeginner

Remove Dead Code

Share

Find and remove unused code

Works with OpenClaude

You are a code refactoring expert. The user wants to identify and safely remove unused code from their codebase.

What to check first

  • Run npm list --depth=0 (Node.js) or check your package manager to see what analysis tools are available
  • Search your IDE/editor for "unused" warnings — most modern editors flag dead code automatically
  • Identify the scope: single file, module, or entire project

Steps

  1. Use your IDE's built-in "Find Unused" feature (VS Code: Cmd/Ctrl+Shift+P → "Find Unused Exports") to auto-detect unreferenced declarations
  2. Run a static analysis tool like eslint with the no-unused-vars rule enabled to catch unused variables, functions, and imports
  3. Use depcheck (run npx depcheck) to identify unused npm dependencies in package.json
  4. Search for dead conditional branches: code inside if (false) blocks or unreachable statements after return
  5. Check for orphaned files: modules imported nowhere else using grep -r "import.*path/to/file" or editor search across the project
  6. Review commented-out code blocks — if they're older than a few commits, remove them (version control has history)
  7. Remove the identified code, then run your test suite (npm test) to verify nothing breaks
  8. Commit the cleanup with a clear message: git commit -m "Remove dead code: delete unused formatHelper function"

Code

// Example: Using ESLint to detect and report unused code
const { ESLint } = require('eslint');
const fs = require('fs');

async function findDeadCode(filePath) {
  const eslint = new ESLint({
    baseConfig: {
      rules: {
        'no-unused-vars': ['error', { args: 'none' }],
        'no-unreachable': 'error',
        'no-constant-condition': 'error'
      }
    }
  });

  const results = await eslint.lintFiles([filePath]);
  
  const deadCodeIssues = results
    .flatMap(result => result.messages)
    .filter(msg => 
      msg.ruleId === 'no-unused-vars' || 
      msg.ruleId === 'no-unreachable'
    );

  deadCodeIssues.forEach(issue => {
    console.log(`${issue.ruleId} at line ${issue.line}: ${issue.message}`);
  });

  return deadCodeIssues;
}

// Example: Removing unused imports programmatically
function removeUnusedImports(code) {
  const ast = require('@babel/parser').parse(code, {
    sourceType: 'module'
  });
  const used = new Set();
  const imported = new Map();

  // First pass: collect imports
  ast.program.body.forEach(node => {
    if

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryRefactoring
Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
refactoringdead-codecleanup

Install command:

curl -o ~/.claude/skills/remove-dead-code.md https://claude-skills-hub.vercel.app/skills/refactoring/remove-dead-code.md

Related Refactoring Skills

Other Claude Code skills in the same category — free to download.

Want a Refactoring skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.