Dead code removal

From aiskillstore/marketplace · View interactive version

Dead code removal is an AI agent skill from the GitHub repository aiskillstore/marketplace. Detects and safely removes unused code (imports, functions, classes) across multiple languages. Use after refactoring, when removing features, or before production deployment. Includes safety checks and validation.

How to use this skill

Skill file

Dead Code Removal

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.

When to Use This Skill

What This Skill Does

  1. Language Detection: Identifies project languages and structure
  2. Entry Point Mapping: Maps entry points and critical paths
  3. Dependency Analysis: Builds dependency graphs and usage patterns
  4. Safe Detection: Identifies unused elements with safety checks
  5. Incremental Removal: Removes code incrementally with validation
  6. Backup Creation: Creates backups before making changes

Helper Scripts

This skill includes Python helper scripts in scripts/:

```bash

python scripts/find_unused_imports.py src/utils.py src/services.py

```

How to Use

Remove Unused Code

Find and remove unused imports and functions in this project
Clean up dead code in src/ directory, but be conservative

Specific Analysis

Check for unused functions in src/utils/ and remove them safely

Analysis Process

1. Language Detection

Identify Project Type:

Detect Entry Points:

2. Build Dependency Graph

Cross-File Dependencies:

Framework Patterns:

3. Detect Unused Elements

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:

4. Safety Checks

Never Remove If:

Framework Preservation:

Python:

JavaScript:

Java:

5. Safe Removal Process

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:

  1. Create temp file with change
  2. Validate syntax
  3. Run tests if available
  4. Apply or rollback

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

Examples

Example 1: Remove Unused Imports

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

Before

import os

import json

from datetime import datetime, timedelta

import requests

After

from datetime import datetime

import requests


### Validation

- ✅ Syntax check passed
- ✅ Tests pass
- ✅ No dynamic imports detected

Example 2: Remove Unused Functions

Input: Find unused functions in src/services/

Output:

## 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

Best Practices

Safety Guidelines

Do:

Don't:

Detection Patterns

Static Analysis:

Validation:

Reporting

Report Should Include:

Related Use Cases

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.