Clean up code by removing dead code, unused imports, and deprecated patterns.
Removes dead code, unused imports, and deprecated patterns from codebases.
/plugin marketplace add adelabdelgawad/fullstack-agents/plugin install adelabdelgawad-fullstack-agents-plugins-fullstack-agents@adelabdelgawad/fullstack-agentsClean up codebase by removing dead code, unused imports, deprecated patterns, and improving code organization.
/optimize cleanup [target]Find unused imports (Python):
# Using grep to find potentially unused imports
for file in $(find . -name "*.py"); do
imports=$(grep "^import\|^from.*import" "$file" | grep -oE "\w+$")
for imp in $imports; do
count=$(grep -c "$imp" "$file")
if [ "$count" -eq 1 ]; then
echo "$file: potentially unused import '$imp'"
fi
done
done
Find unused imports (TypeScript):
# Check for unused imports in TypeScript
npx eslint --rule 'no-unused-vars: error' --ext .ts,.tsx .
Find unreachable code:
# Functions never called
grep -rh "^def \|^async def " --include="*.py" | sed 's/def \([a-z_]*\).*/\1/' | while read func; do
count=$(grep -rh "$func(" --include="*.py" | wc -l)
if [ "$count" -lt 2 ]; then
echo "Potentially unused function: $func"
fi
done
Find commented-out code:
grep -rn "^#.*def \|^#.*class \|^#.*if \|^#.*for " --include="*.py" | head -20
Find deprecated datetime usage:
grep -rn "datetime.utcnow()\|datetime.now()" --include="*.py"
# Should use: datetime.now(timezone.utc)
Find deprecated typing:
grep -rn "from typing import Optional\|from typing import List" --include="*.py"
# Python 3.10+: Use list[], dict[], | None instead
## Code Cleanup Report
**Target:** {scope}
**Generated:** {timestamp}
### Summary
| Category | Items Found | Fixed |
|----------|-------------|-------|
| Unused Imports | 23 | 23 |
| Dead Code | 8 | 8 |
| Deprecated Patterns | 12 | 12 |
| Commented Code | 15 | 15 |
| Empty Files | 2 | 2 |
### Unused Imports Removed
**File:** `api/services/product_service.py`
```python
# Removed
from typing import Optional, List, Dict # Unused: Dict
from sqlalchemy import select, and_, or_ # Unused: and_, or_
import json # Unused
After:
from typing import Optional, List
from sqlalchemy import select
File: api/utils/helpers.py
Removed unused function:
# This function was never called anywhere
def format_phone_number(phone: str) -> str:
"""Format phone number - REMOVED (unused)"""
...
File: api/services/user_service.py:67
Before:
user.created_at = datetime.utcnow() # Deprecated
After:
from datetime import datetime, timezone
user.created_at = datetime.now(timezone.utc)
File: api/schemas/user_schemas.py
Before:
from typing import Optional, List
class UserResponse(BaseModel):
tags: Optional[List[str]] = None
After:
class UserResponse(BaseModel):
tags: list[str] | None = None
Files cleaned:
api/v1/users.py:45-67 - Old implementation (23 lines)api/services/order_service.py:123-145 - Debug code (22 lines)db/models.py:89-95 - Old field definition (7 lines)api/utils/__init__.py (empty, no longer needed)tests/fixtures/__init__.py (empty, no longer needed)Consolidated imports:
Removed redundant code:
| File | Changes |
|---|---|
api/services/product_service.py | 3 unused imports |
api/services/user_service.py | 2 deprecated patterns |
api/utils/helpers.py | 1 dead function |
api/v1/users.py | 23 lines commented code |
api/schemas/user_schemas.py | 5 typing updates |
Add linting to CI/CD:
- run: ruff check .
- run: mypy .
Add pre-commit hooks:
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
hooks:
- id: ruff
Consider these tools:
vulture - Find dead Python codeautoflake - Remove unused importsisort - Sort importsDesigns feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences