Find and remove unused code
✓Works with OpenClaudeYou 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
- Use your IDE's built-in "Find Unused" feature (VS Code: Cmd/Ctrl+Shift+P → "Find Unused Exports") to auto-detect unreferenced declarations
- Run a static analysis tool like
eslintwith theno-unused-varsrule enabled to catch unused variables, functions, and imports - Use
depcheck(runnpx depcheck) to identify unused npm dependencies inpackage.json - Search for dead conditional branches: code inside
if (false)blocks or unreachable statements afterreturn - Check for orphaned files: modules imported nowhere else using
grep -r "import.*path/to/file"or editor search across the project - Review commented-out code blocks — if they're older than a few commits, remove them (version control has history)
- Remove the identified code, then run your test suite (
npm test) to verify nothing breaks - 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
Related Refactoring Skills
Other Claude Code skills in the same category — free to download.
Extract Function
Extract code blocks into well-named functions
Rename Symbol
Safely rename variables, functions, classes across codebase
Simplify Conditionals
Simplify complex if/else chains and nested conditions
Extract Component
Extract UI code into reusable components
DRY Refactor
Find code duplication and extract shared logic
Promise to Async
Convert Promise chains to async/await
Class to Functions
Convert class-based code to functional style
Callback to Promise
Convert callback-based code to Promises
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.