Identify and analyze unused or redundant code including unused functions/methods, unused variables/imports, unreachable code, and redundant conditions. Use when cleaning up codebases, improving maintainability, reducing technical debt, or conducting code quality audits. Analyzes Python code using AST analysis and produces markdown reports listing dead code locations with line numbers, severity ratings, and recommendations. Triggers when users ask to find dead code, remove unused code, identify unused imports, find unreachable code, or clean up redundant logic.
81
75%
Does it follow best practices?
Impact
93%
2.06xAverage score across 3 eval scenarios
Passed
No known issues
Optimize this skill with Tessl
npx tessl skill review --optimize ./skills/dead-code-eliminator/SKILL.mdSystematically identify unused or redundant code in Python codebases to improve maintainability, reduce confusion, and eliminate technical debt.
Define what to analyze:
Questions to ask:
Determine analysis scope:
# Check project structure
ls -la
# Count Python files
find . -name "*.py" | wc -l
# Identify test directories
find . -type d -name "*test*"Use multiple detection strategies to find different types of dead code.
Use the bundled script for AST-based analysis:
# Scan entire project
python scripts/find_unused_functions.py /path/to/project
# Exclude specific directories
python scripts/find_unused_functions.py /path/to/project tests,venv,docsWhat it detects:
Limitations:
Use the bundled script to identify unused imports:
# Scan single file
python scripts/find_unused_imports.py /path/to/file.py
# Scan entire directory
python scripts/find_unused_imports.py /path/to/project
# Exclude directories
python scripts/find_unused_imports.py /path/to/project venv,.venv,testsWhat it detects:
from X import Y statementsLeverage Python ecosystem tools for comprehensive analysis:
vulture - Finds unused code:
# Install
pip install vulture
# Run on project
vulture /path/to/project
# Exclude directories
vulture /path/to/project --exclude venv,tests
# Set minimum confidence (0-100)
vulture /path/to/project --min-confidence 80autoflake - Focuses on imports and variables:
# Install
pip install autoflake
# Check for unused imports
autoflake --check --imports /path/to/file.py
# Check unused imports and variables
autoflake --check --remove-all-unused-imports --remove-unused-variables /path/to/file.py
# Recursive scan
autoflake --check -r /path/to/projectpylint - General linting including dead code:
# Install
pip install pylint
# Check for unused variables, imports, functions
pylint /path/to/project --disable=all --enable=unused-import,unused-variable,unreachableRead the code to identify patterns:
Unreachable code:
return statementsbreak, continue, or raiseRedundant conditions:
else after returnLook for:
# Find code after return statements (basic pattern)
grep -A 3 "return" **/*.py | grep -v "^--$"
# Find functions with "old" or "legacy" in name
grep -r "def.*old\|def.*legacy" .
# Find TODO comments about removal
grep -r "TODO.*remove\|FIXME.*delete" .Organize dead code by type and priority.
See dead-code-patterns.md for comprehensive pattern catalog.
Priority: High (easy to remove, low risk)
Examples:
import os but os is never usedfrom typing import List, Dict but only List is usedPriority: Medium to High
Subcategories:
Caution - May be intentional:
Priority: High (indicates bugs or confusion)
Examples:
returnraisePriority: Medium
Examples:
else after returnPriority: Low to Medium
Examples:
Before reporting, verify that identified code is truly dead.
Check for dynamic usage:
# Code may appear unused but is called dynamically
handlers = {
'process': process_handler, # Looks unused but isn't
}
# Or via getattr
handler = getattr(module, function_name)Check for external usage:
Check for framework conventions:
# Django signal handlers
@receiver(post_save, sender=User)
def user_saved(sender, instance, **kwargs): # May appear unused
pass
# Pytest fixtures
@pytest.fixture
def sample_data(): # Used by tests but not "called" directly
return {"key": "value"}Verify with grep:
# Search for function name in entire codebase
grep -r "function_name" .
# Search in quotes (dynamic calls)
grep -r "'function_name'\|\"function_name\"" .
# Search in setup.py or config files
grep -r "function_name" setup.py pyproject.tomlCreate a structured markdown report of findings.
Project: [Project Name] Analyzed: [Date] Scope: [Directories analyzed] Excluded: [Excluded directories]
Total: Z dead code instances found
Code that can never execute - should be removed immediately.
Location: src/utils.py:45-47
Code:
def process_data(data):
if not data:
return None
logging.warning("Empty data") # UNREACHABLE
validate(data) # UNREACHABLERecommendation: Remove lines 46-47 (unreachable after return).
Impact: Misleading code that suggests validation happens but doesn't.
src/main.pyLines:
import os (unused)from typing import Dict, Tuple (only Dict is used)import re (unused)Recommendation: Remove unused imports. Update line 5 to from typing import Dict.
Functions that appear unused but should be verified before removal.
Location: src/helpers.py:89
Function: format_timestamp(ts: int) -> str
Analysis:
Verification needed:
Recommendation: If not part of public API, remove. Otherwise, document it.
Locations:
src/processor_a.py:45-52src/processor_b.py:78-85Code: Both files contain identical validation logic.
Recommendation: Extract common logic into shared utility function.
Impact: Maintenance burden - changes must be duplicated.
Location: src/validator.py:123-127
Code:
def check_status(value):
if value > 0:
return "positive"
else: # Unnecessary
return "negative"Recommendation: Remove else clause (implicit after return).
Impact: Minor - slightly less readable but no functional impact.
Location: src/calculator.py:56
Code:
def compute(a, b):
total = a + b # Assigned but never used
return a * bRecommendation: Remove unused total variable.
These appear unused but may be called dynamically. Manual verification needed.
handle_create()Location: src/handlers.py:34
Reason for caution: File contains handler registry suggesting dynamic dispatch.
Code pattern:
HANDLERS = {
'create': handle_create,
'update': handle_update,
}Recommendation: Verify this is registered and used. If confirmed unused, remove.
Add to CI/CD pipeline:
# Add to pre-commit hook or CI
vulture src/ --min-confidence 80
autoflake --check -r src/Configure IDE:
Code review checklist:
[If needed, include full lists of all findings organized by file]
Share the report with the team and get feedback.
Present clearly:
Be cautious about:
Request feedback:
Start conservatively:
Use multiple detection methods:
Prioritize by risk:
Consider the context:
Prevention is better than cure:
Be aware of code that appears dead but isn't:
1. Dynamic dispatch:
handler = getattr(module, f"handle_{action}")2. Entry points:
# setup.py
entry_points={
'console_scripts': ['tool=module:main_function']
}3. Pytest fixtures:
@pytest.fixture
def sample_data(): # Used by tests implicitly
return data4. Django signals:
@receiver(post_save, sender=Model)
def handle_save(sender, instance, **kwargs): # Called by framework
pass5. Decorators and metaclasses:
class Meta:
def __init_subclass__(cls): # Called implicitly
register(cls)6. Public API functions:
# In library code - may be used by external code
def public_function(): # Appears unused internally
passFor comprehensive dead code patterns and detection strategies, see dead-code-patterns.md.
0f00a4f
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.