Detects and safely removes unused code (imports, functions, classes) across multiple languages. Use after refactoring, when removing features, or before production deployment...
This skill safely identifies and removes unused code across multiple programming languages. It includes comprehensive safety checks to prevent removing code that's actually needed.
This skill includes Python helper scripts in scripts/:
find_unused_imports.py: Uses AST parsing to accurately detect unused imports in Python files. Outputs JSON with unused imports and line numbers.
python scripts/find_unused_imports.py src/utils.py src/services.py
Find and remove unused imports and functions in this project
Clean up dead code in src/ directory, but be conservative
Check for unused functions in src/utils/ and remove them safely
Identify Project Type:
pyproject.toml, setup.py, requirements.txtpackage.json, tsconfig.jsonpom.xml, build.gradlego.modCargo.tomlDetect Entry Points:
main.py, __main__.py, app.py, run.pyindex.js, main.js, server.js, app.jsMain.java, *Application.java, *Controller.java*.config.*, settings.*, setup.*test_*.py, *.test.js, *.spec.jsCross-File Dependencies:
Framework Patterns:
Using Helper Script:
The skill includes a Python helper script for finding unused imports:
# Find unused imports in Python files
python scripts/find_unused_imports.py src/utils.py src/services.py
Unused Imports:
# Python: AST-based analysis
import ast
# Track: Import statements vs actual usage
# Skip: Dynamic imports (importlib, __import__)
// JavaScript: Module analysis
// Track: import/require vs references
// Skip: Dynamic imports, lazy loading
Unused Functions/Classes:
Never Remove If:
getattr(), eval(), globals() usage detectedwindow[], this[], dynamic import() usage@Component, @Service)Framework Preservation:
Python:
JavaScript:
Java:
Backup Creation:
backup_dir="./unused_code_backup_$(date +%Y%m%d_%H%M%S)"
cp -r . "$backup_dir" 2>/dev/null || mkdir -p "$backup_dir" && rsync -a . "$backup_dir"
Incremental Removal:
Validation:
# Python
python -m py_compile file.py
python -m pytest
# JavaScript
npx eslint file.js
npm test
# Java
javac -Xlint file.java
mvn test
Input: Clean up unused imports in src/utils/helpers.py
Output:
## Dead Code Removal: src/utils/helpers.py
### Analysis
- **File**: `src/utils/helpers.py`
- **Total imports**: 8
- **Unused imports**: 3
### Unused Imports Found
1. `import os` - Line 2 (not used anywhere)
2. `import json` - Line 3 (not used anywhere)
3. `from datetime import timedelta` - Line 5 (not used anywhere)
### Safe to Remove
✅ All 3 imports are safe to remove (no dynamic usage detected)
### Changes
```python
# Before
import os
import json
from datetime import datetime, timedelta
import requests
# After
from datetime import datetime
import requests
### Example 2: Remove Unused Functions
**Input**: Find unused functions in `src/services/`
**Output**:
```markdown
## Dead Code Removal: src/services/
### Analysis
- **Files analyzed**: 12
- **Functions found**: 45
- **Unused functions**: 2
### Unused Functions
**1. `formatOldDate()` in `src/services/utils.js`**
- **Location**: Line 34-42
- **Status**: ✅ Safe to remove
- **Reason**: No references found, not exported, not used in tests
**2. `legacyAuth()` in `src/services/auth.js`**
- **Location**: Line 78-95
- **Status**: ⚠️ Preserved (framework pattern)
- **Reason**: Referenced in route configuration (line 12)
### Summary
- **Removed**: 1 function (`formatOldDate`)
- **Preserved**: 1 function (framework usage)
- **Lines removed**: 9
- **Size reduction**: ~300 bytes
Do:
Don't:
Static Analysis:
Validation:
Report Should Include: