Dead Code: Find It, Prove It, Delete It

Dead Code: Find It, Prove It, Delete It

Dead code taxes every reader and keeps dependencies alive. A taxonomy from unused exports to expired feature flags, plus confidence tiers for deleting with evidence, not belief.

Dead code is not neutral — it taxes every reader who passes through the file, misleads anyone debugging production behavior, and keeps dependencies, along with any known vulnerabilities they carry, in the tree for a path nothing reaches. The taxonomy runs from unused exports to expired feature flags, a recurring modern source. Static analysis catches the easy cases but has honest blind spots; runtime evidence closes the gap. Confidence tiers — provably unreachable, evidence-dead, belief-dead — dictate a different action each, and version control, not a comment block, is the archive that answers "we might need it."

What dead code costs

A block of code that never executes is not free just because it never runs. It is parsed by every tool that touches the file, considered by every reader who scrolls past it, and mentally simulated by anyone trying to understand what the function does. Multiply that by the number of engineers who will open the file over its lifetime, and an unreachable branch has a real, recurring cost even though the interpreter skips it.

The second cost is sharper: dead code misleads about what actually runs. Picture a reader debugging a production incident who sees a conditional, assumes it is live, and spends an afternoon reasoning about a branch that has been unreachable for two years. The code is not lying on purpose, but it is lying by omission — nothing in the file distinguishes a path that matters from one that stopped mattering long ago.

The third cost is the least visible and the most concrete: unless a build step strips it, an unused code path still ships. (Tree shaking in a bundler such as webpack, for example, can drop unused ES module exports from a production bundle.) Even when the bundle leaves it out, the code stays in the repository: it still imports its dependencies, still counts toward the test surface a team maintains, and if it calls out to a library with a known vulnerability, that library stays in the dependency tree the team has to track and patch. Nothing in the current build stops a future change from calling the dead path again.

None of this requires the code to be actively harmful. The cost is cumulative and quiet: reader tax, misinformation about live behavior, and a dependency footprint that outlives its own purpose.

The dead-code taxonomy

Dead code arrives in more shapes than a single "delete unused stuff" instinct suggests.

Unused exports and functions are the simplest species — reachable by nothing, kept because nobody has had a reason to remove them. Unreachable branches are conditions that can no longer be true, usually left behind by an earlier change that removed the state that used to trigger them. Commented-out blocks are the most visible species and the least defensible: they sit in the file in plain sight, and version control already has every committed version of the code they hide, making the comment a pure reader tax with no offsetting benefit.

Expired feature flags deserve their own category because they are a recurring modern source of dead code. A flag pinned at 100 percent (or 0 percent) for a long time is not just an unused branch — it is a dead branch plus a live conditional that every reader still has to evaluate, because nothing in the code signals that the decision was made permanently long ago. Pete Hodgson's article on feature toggles notes that savvy teams view their toggles as inventory which comes with a carrying cost and work to keep that inventory as low as possible; to keep the number manageable, it adds, a team must be proactive in removing flags that are no longer needed. By that measure, an expired flag is inventory that stopped earning its keep.

Rounding out the list: stale function parameters nobody passes a meaningful value for anymore, config keys nothing reads, and orphaned assets referenced by nothing. Each is smaller than a whole function, which is exactly why each tends to survive longer — too minor to notice individually, and numerous enough that together they add up.

Proving deadness honestly

Static analysis is the first and cheapest tool, and it earns its keep on the easy cases: a function with zero references anywhere in the codebase is about as close to certain as this gets. But static analysis has honest blind spots, and pretending otherwise is how confident deletions turn into incidents. Dynamic dispatch can defeat it, because the caller is resolved at runtime rather than visible in the source graph. Reflection can defeat it for the same reason — a function invoked by name from a string is invisible to a tool that only follows explicit references. String-keyed lookup tables have the identical problem: a handler registered under a key and dispatched later never shows up as a direct call site. And external callers — another service, a public API consumer, a scheduled job defined outside the repository — are invisible to any analysis that only sees the code in front of it.

Runtime evidence closes exactly this gap. Logs, telemetry, or a temporary counter attached to the suspected path answer a concrete question with data instead of belief: has this path executed within a defined observation window. Where static analysis says "I cannot see a caller," runtime evidence says "and here is confirmation that none fired in production either."

A more aggressive option is the removal test — delete the code and watch what breaks. This is legitimate only under real preconditions: the change is easily reversible, the affected surface is observable enough that a break shows up quickly, and the deployment context can tolerate the experiment. Removing a background job's dead branch in a service with good monitoring and a fast rollback path is a reasonable test. Doing the same to code on a critical payment path, with observability that would only catch the failure days later, is not — the honesty of "delete it and watch" depends entirely on how fast and how visibly the watching happens.

Together, static analysis and runtime evidence sort a candidate into one of three confidence tiers, each with a different action:

TierWhat it meansAction
Provably unreachableStatic analysis confirms zero references, and none of the known blind spots apply (no dynamic dispatch, no reflection, no string-keyed lookup, no external callers)Delete now
Evidence-deadNo static reference found, and runtime evidence over an honest observation window shows no execution, though a blind spot cannot be fully ruled outDelete with monitoring and a fast rollback path in place
Belief-deadLooks unused, feels unused, but no runtime evidence has been gathered where the blind spots applyGather evidence first, or run the removal test under safe preconditions — this is a claim, not yet a fact

Treating a belief-dead block as though it were provably unreachable is the shortcut that turns a routine cleanup into an incident.

Deleting with confidence

Once deadness is proven, the deletion itself should be handled deliberately, not apologetically.

Version control is the archive. This is the definitive answer to the instinct to leave a commented-out copy "just in case": a deleted function is one log search away from full recovery, with its history intact, while a commented-out block is a permanent reader tax that pays off only if someone un-comments it. If the worry is losing the code, the worry is already solved by the tool that has been solving it for decades.

Deletion commits are mechanical and should stay that way. A pure-deletion diff — this function, its imports, nothing else touched — is quick to review, because the reviewer's only question is whether the deletion claim holds, not whether new logic is correct. Mixing a deletion into an unrelated feature commit forfeits that speed for no benefit; the deletion deserves its own diff.

Delete the whole organism, not just the visible part. That means the function itself, its tests, any documentation that references it, the config keys it read, and its export from any index file. A half-deleted feature — the function is gone but a stale reference to it lingers in a doc or a config schema — is its own new species of confusion, arguably worse than leaving the whole thing alone, because now the remaining traces point at something that no longer exists.

text
git rm src/billing/legacyDiscountEngine.ts
git rm src/billing/legacyDiscountEngine.test.ts
# remove the now-unused export from src/billing/index.ts
# remove the LEGACY_DISCOUNT_ENABLED key from config schema
# search docs/ for references to legacyDiscountEngine and remove them

Each of those steps closes a door the function's presence had left open.

Answering the resistance

"We might need it" is a familiar objection, and it deserves a direct answer rather than a shrug: version control keeps the code retrievable from history for as long as that history is kept, so the real question is not whether to keep it but where. A repository history that can restore any committed line on demand is a cheaper place to keep it than a live file that every reader has to step around in the meantime.

Sunk cost has no claim on the decision either. The effort that went into building the code is spent regardless of whether it stays or goes — that ledger closed the moment the work was done. The only things actually on the table are future cost (the ongoing reader tax and dependency exposure) and future benefit (whether anything will plausibly call this code again), and sunk effort answers neither question.

There is an honest counter-case worth taking seriously: code that is genuinely seasonal — a year-end reconciliation routine, a holiday pricing rule — is not dead just because it did not run recently. The confidence-tier discipline covers this too: a telemetry window has to be long enough to actually see the pattern it is trying to rule out, and "no calls in 30 days" says nothing reliable about code that is only supposed to run once a year. The seasonal case is not a reason to avoid deletion discipline; it is a reason to set the observation window honestly before applying it.

Project Inquiry