Dead code finder
Goal
Produce a list of removal candidates, each with the evidence that nothing reaches it and a confidence level. Nothing is deleted without that evidence, and the user decides what goes.
When to use
- The user asks for unused code, files or exports.
- A cleanup precedes a refactor or a dependency upgrade.
- To remove a whole feature that is still wired in, plan it as a refactor with
use_skillandrefactor. - To find the riskiest code rather than the unused code, use
use_skillwithcomplexity-hotspots. - A bundle or install size has grown and unused dependencies are suspected.
Steps
- Check for tooling first. Use
read_fileonpackage.json,pyproject.tomlor the build config and look for unused-code tools (knip, ts-prune, vulture,go vet, compiler warnings). Run the one that exists withrun_commandand keep its output as the starting list. - If no tool exists, build candidates with
rg: take each exported symbol or top-level function and count references outside its own definition. - For every candidate, check the references that tools miss:
- Dynamic lookups by string (
obj[name],getattr,require(variable)). - Registration by convention: route files, plugin folders, migrations, command handlers, test discovery.
- Config, JSON, YAML, HTML, templates and scripts that name the symbol or path.
- Public API that outside code may import.
- Reflection, serialisation and decorators.
- Dynamic lookups by string (
- Check history cheaply with
run_commandandgit log -S "<name>" --oneline, which shows when it was last used and why it was added. - Classify each candidate: unreferenced anywhere (high confidence), referenced only by tests or by other dead code (medium), possibly used dynamically or externally (low).
- For unused dependencies, search for imports and for use in scripts and config before listing them. Check the live docs with
read_urlif a package works by side effect. - Report. If the user then asks for deletion, remove in small commits, then run build and tests with
run_commandafter each. - Group the high-confidence list into small deletion batches by folder, so each batch can be reverted alone.
- After any deletion the user approves, run the build, typecheck and tests with
run_command, then search again, since removal exposes new unused code.
Gotchas
- Zero grep hits do not prove deadness. Dynamic dispatch, config and external consumers hide references.
- Code used only by tests is not production-dead, but deleting it breaks those tests. Say which case it is.
- A function called only by other dead code is dead too, but only after the first layer goes. Work in layers.
- Feature flags that are always off look alive to a grep. Check the flag's current value and where it is set.
- Exported from a library is a different question from unused in this repo. Ask who else imports the package.
- Commented-out code and unused files in
scripts/ordocs/often have a human who still wants them.
Output
- A table: item, location, evidence, confidence, suggested action.
- Items grouped by confidence, high first.
- What you could not check, such as external consumers, so the user can confirm.
- Suggested batch order if deletion goes ahead.
