# unused-code-cleaner

A subagent that finds unused imports, functions and classes and removes them one at a time, validating after each step. Handy after a refactor or before a production deploy.

- subagent · `type=agent` · Claude Code · 1990 installs
- Author: davila7
- License: not stated · policy=link-only · description only; the file stays with the author
- Source: https://github.com/davila7/claude-code-templates/tree/HEAD/cli-tool/components/agents/development-tools/unused-code-cleaner.md
- Updated: 2026-10-24
- HTML: https://affpapa.org/en/nevibecode/agents/unused-code-cleaner

## What it does

The subagent runs in stages: it detects project languages and structure, maps entry points, builds a dependency graph, then removes unused elements incrementally with syntax and test validation. If validation fails, the change is rolled back. It has `Read`, `Write`, `Edit`, `Bash`, `Grep` and `Glob` at its disposal.

Before touching anything it copies the directory to a backup folder. Analysis leans on `npx depcheck` and `npx ts-unused-exports` for JS/TypeScript, `py_compile` and `pytest` for Python, `javac` and `mvn test` for Java. Dynamic access patterns stay untouched: `getattr()`, `eval()`, `window[]`, Java reflection. So do framework pieces — Django models, Flask routes, React components, Spring beans — along with entry points like `main.py` or `index.js`, config files and tests.

Safety wins over aggressive cleanup here. When the purpose of a piece of code is unclear, the subagent keeps it and flags it for manual review. That means one pass won't leave you with a spotless codebase. What you get instead is no lost code that a template string or config file still calls.

## Install

### Claude Code (native platform)

1. **claude-code-templates**
```bash
npx claude-code-templates@latest --agent development-tools/unused-code-cleaner
```

2. **Manually: put the files in place**
- File: `.claude/agents/unused-code-cleaner.md` — For all projects use ~/.claude/agents/. Claude delegates by the description field or an @-mention

### Codex

**Manually: put the files in place**
- File: `.codex/agents/unused-code-cleaner.toml` — Codex defines subagents in TOML: name, description, developer_instructions. Move the Markdown text into developer_instructions by hand. For all projects use ~/.codex/agents/

### Cursor

**Manually: put the files in place**
- File: `.cursor/agents/unused-code-cleaner.md` — Cursor also reads .claude/agents/

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/davila7/claude-code-templates/tree/HEAD/cli-tool/components/agents/development-tools/unused-code-cleaner.md

## When to use it
- strip imports left behind after a module move
- find functions and classes nothing references anymore
- clean the project before a production deploy
- check whether tests still pass after a removal
- roll back a change when syntax or tests fail

## Example prompts
- scan the project for unused code and show me the report
- remove dead imports in src and run tests after each step
- back up the repo, then clean unused functions and flag anything uncertain for review

## Similar
- [code-reviewer](https://affpapa.org/en/nevibecode/agents/anthropic-code-reviewer.md) — subagent · 31050 installs
- [code-reviewer](https://affpapa.org/en/nevibecode/agents/code-reviewer.md) — subagent · 31050 installs
- [security-auditor](https://affpapa.org/en/nevibecode/agents/security-auditor.md) — subagent · 10718 installs
- [test-engineer](https://affpapa.org/en/nevibecode/agents/test-engineer.md) — subagent · 9715 installs
- [code-reviewer](https://affpapa.org/en/nevibecode/agents/voltagent-community-code-reviewer.md) — subagent · 31050 installs
- [debugger](https://affpapa.org/en/nevibecode/agents/debugger.md) — subagent · 13459 installs

From the same set: [davila7/claude-code-templates](https://affpapa.org/en/nevibecode/by/davila7/claude-code-templates.md) — 224 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
