Remove Dead Code
Category G — Hygiene and cleanup
Delete code that has no effect on observable behavior because it cannot be reached or is no longer used.
Dead code is liability: it increases reading cost, misleads future changes, and creates false confidence that behavior matters when it does not.
Motivation
The smell
- Unused symbols. Functions, methods, classes, variables, imports, constants, or files with no references.
- Unreachable branches. Conditions that can never be true, returns after returns, or handlers for impossible states.
- Obsolete parameters. Parameters accepted but never used, especially after behavior changes.
- Commented-out or legacy remnants. Old code kept "just in case" when version control already preserves history.
- Dead feature flags/compatibility paths. Paths no longer reachable in supported configurations.
Fowler's bias: when you are sure code is not used, delete it; version control can bring it back if needed.
The design principle
- Less code, less confusion. Readers should not spend effort understanding behavior that never happens.
- Honest codebase. The remaining code should represent real behavior and real contracts.
- Safe deletion. Removal is behavior-preserving only when the code is truly dead in all relevant contexts.
- Behavior preservation. Deleting truly dead code does not change observable behavior; deleting "apparently dead" code can.
Remove Dead Code is not:
- deleting code you do not understand yet;
- removing public API used by external consumers;
- hiding uncertainty by deleting instead of investigating.
When not to remove
- It may be used dynamically. Reflection, plugin loading, string-based dispatch, serialization, dependency injection, templates, or framework conventions.
- It is a public/external contract. Exported API, CLI flags, config keys, wire formats, or library entry points may have out-of-repo callers.
- It is required by tests/build/runtime conventions not visible from a simple reference search.
- It is intentionally unreachable for safety (e.g., exhaustive-check guard) and conveys meaning; consider whether it is truly dead.
- You are not sure. Investigate first. Ask before deleting ambiguous cases.
Mechanic
Before you start
- Read the file and identify the suspected dead code. Distinguish "unused locally" from "unused everywhere relevant".
- Search for references: calls, imports, exports, string references, config, tests, docs, reflection/framework conventions.
- Find existing checks. Use tests/type checker/linter/build as available. Dead-code removal often benefits from static analysis, but do not assume it is complete.
- Work in small deletions. One dead symbol/path at a time is easier to verify and revert.
Steps
- Pick one candidate. Prefer the clearest local dead code: unused local variable, unused private helper, unreachable branch after obvious return.
- Prove or qualify deadness. Gather evidence. If evidence is incomplete, mark risk and ask.
- Delete the code. Remove the symbol and its now-dead support code: unused imports, unused helpers, stale comments, dead parameters if safe.
- Run checks. Use existing tests/type checks/lint/build. If anything fails, determine whether the code was not dead.
- Repeat in dependency order. Removing one dead function may make imports or helpers dead; clean them up in follow-up steps.
- Report clearly. State what was removed, what evidence supported it, and any remaining uncertain candidates.
Assessing deadness
| Candidate | Evidence to look for | Risk |
|---|---|---|
| Unused local variable | No reads after assignment; no side-effect dependency | Low |
| Unused private function/method | No references in codebase; not reflected/dynamically called | Low-medium |
| Unused import | Symbol not referenced; no side-effect import needed | Low |
| Unused parameter | No reads; not required by interface/override/external signature | Medium |
| Unreachable branch | Provably impossible condition or control flow | Medium |
| Unused exported/public symbol | No in-repo refs, but may have external consumers | High |
| Dead file/module | No imports/references; check build/config/entry points | High |
Rules that keep the move safe:
- Do not delete code based only on a gut feeling.
- Preserve side effects. An "unused" value may come from a call that must still happen.
- Check interfaces/overrides before removing parameters or methods.
- Check external/public contracts before removing exports.
- Prefer small, reversible deletions.
Example
Before
def build_greeting(name: str, formal: bool) -> str:
display_name = name.strip().title()
if formal:
return f"Good day, {display_name}."
return f"Hello, {display_name}!"
def build_farewell(name: str) -> str:
display_name = name.strip().title()
return f"Goodbye, {display_name}."
If build_farewell has no references, tests, exports, or dynamic use, it is dead code.
After
def build_greeting(name: str, formal: bool) -> str:
display_name = name.strip().title()
if formal:
return f"Good day, {display_name}."
return f"Hello, {display_name}!"
The observable behavior of the used code is unchanged. The codebase is smaller and more honest.
A non-example
def plugin_hook(event):
return handle(event)
Even with no in-repo callers, a plugin hook may be loaded by name. Do not remove it without confirming the contract.
Related refactorings
- Inline Function — can leave behind an unreferenced function that becomes dead code.
- Move Function / Move Field — old locations may become dead after migration.
- Substitute Algorithm — replacing an algorithm can make old helpers dead.
- Extract Function — sometimes reveals dead branches or unused values; remove them only when proven dead.
Running this skill on a file
When invoked with @remove_dead_code:
- Confirm the target. Use the file named by the user. If none is named, ask for one. Read it completely.
- Inventory candidates. For each candidate, report:
- symbol/location,
- kind of dead code (unused function, variable, import, parameter, unreachable branch, dead file),
- evidence for deadness (reference search, call graph, type/lint signal),
- contract/dynamic-use risk,
- risk: low (local/private, strong evidence), medium (internal but indirect), high (public/exported/dynamic/serialized).
- Propose before editing. Present deletions in safest-first order. Wait for approval. Do not change files on the first pass.
- Apply one deletion at a time. Remove the dead code and any clearly dead support code it leaves behind.
- Run existing checks after each step. If checks fail, treat it as evidence the code may not have been dead; revert and report.
- Report. Say what was removed, evidence used, what was not verified, and any suspicious candidates left untouched.
- Stop and ask when deadness cannot be proven, when public/dynamic use is plausible, or when removal would change behavior.
Checklist
- Deadness is supported by evidence, not guesswork.
- Side effects were preserved; only truly dead code was removed.
- Public/dynamic/contract risks were considered.
- Deletions were small and verified.
- Checks pass, or the unverified risk is stated.