← Refactoring and maintenance guides

How to remove dead code with SpecDD

How-To Refactoring and maintenance Intermediate 1151008HOWTO-1151008

HOWTO-1151008Refactoring and maintenanceIntermediate

This guide shows you how to remove dead code with SpecDD in a spec-driven development workflow.

Dead code removal is risky when the code looks unused but still represents specified behavior, compatibility, migration support, or a boundary rule. SpecDD gives you a way to separate dead implementation from live contract.

The safe deletion path is: prove the behavior is obsolete, update the governing spec, remove code and tests together, and verify no stale specs still describe deleted behavior.

Short answer

Before deleting code, trace the specs that govern it. If the behavior is still required by Must, Scenario, Example, public contract sections, or Done when, do not delete it until the spec is changed or retired. If the behavior is obsolete, update or remove the owning spec, delete code and tests, update references, run checks, and confirm no stale rules remain.

When to use this guide

Use this guide when:

Steps

1. Prove the behavior is obsolete

Ask:

Do not delete code only because it has low usage or looks old.

2. Find governing specs

Trace the code to:

Look for these section labels:

Must:
Scenario:
Example:
Exposes:
Returns:
Raises:
Done when:
Tasks:

Any of these may still require the behavior.

3. Update or remove spec rules

If the behavior is intentionally removed, update the owner:

Must not:
  Use the legacy trip export path for new exports.

Tasks:
  [x] Remove legacy trip export code after v2 export adoption.

If the whole spec is obsolete, retire the spec. If only one behavior is obsolete, remove or revise the specific rule and leave the rest.

4. Delete code and tests together

Remove:

Keep tests that still prove current behavior or compatibility.

5. Update references and paths

Search for the deleted code and spec names. Update:

Leaving stale references makes future maintenance harder.

6. Run checks

Run:

If checks fail because tests still expect deleted behavior, decide whether the test is stale or the deletion was wrong.

Example removal plan

Target: Legacy trip export

Evidence:
  v2 export is released
  no active callers use legacy command
  deprecation removal criterion is satisfied
  legacy behavior removed from Export API spec

Delete:
  legacy export implementation
  legacy-only fixtures
  legacy export tests

Keep:
  v2 export contract tests
  compatibility note in release docs

Checks:
  specdd lint export directory
  export API contract tests
  project build

Common mistakes

How to verify the result

Dead code removal is safe when:

← Refactoring and maintenance guides