Elon Musk
Delete-first refactoring. Shrink the system first, then optimize what survives.
Executive Summary
- Delete aggressively (often 70-90%) behind flags
- Preserve externally required behavior (APIs, SLOs, safety controls)
- Re-add only what measured outcomes require
- Optimize after simplification
- Automate last
Preconditions
Do not run this playbook unless all are true:
- CI is green
- Characterization and contract tests exist for the target area
- Feature flags and rollback path are ready
- Owners for requirements and risk sign off
Operating Order
- Confirm requirement owners and explicit keep-list
- Delete first (if little is deleted, challenge scope)
- Simplify the remaining architecture
- Optimize only measured bottlenecks
- Automate only after process stability
Git Safety Gates (Mandatory)
Before any large deletion:
- Create an isolated branch:
refactor/<area>-delete-first - Start from a clean working tree
- Create a recoverable checkpoint:
- lightweight tag:
pre-delete-<area>-<YYYYMMDD> - optional backup branch:
backup/<area>-pre-delete
- lightweight tag:
- Use small, reviewable commits with intent-first messages, for example:
refactor!: delete deprecated parser pathtest: add characterization coverage for billing flow
- Open small PRs with mandatory reviewers and green CI
Hard rules:
- No deletion rampage on
main - No force-push to shared branches during refactor
- No removal of auth/audit/PII/security controls without explicit review and tests
Workflow
- Baseline
- Capture current behavior, perf, and error metrics
- Write keep-list: public APIs, SLOs, compliance/safety paths
- Delete behind flags
- Remove dead/duplicate/no-owner/untestable code first
- Target large reduction (70-90%) where safe
- Verify
- Run characterization + contract tests
- Enforce CI gates before merge
- Decide with data
- Re-add only if user impact, reliability, or contractual behavior requires it
- Optimize survivors
- Automate stable flow last
Keep vs Delete Heuristic
Keep:
- Public API and compatibility contracts
- Safety/regulatory behavior
- Proven hot paths with measurable value
Delete first:
- Unused code
- Duplicate logic
- Code with no owner
- Code impossible to test or reason about
Rollback and Recovery
- Every risky deletion is flag-guarded
- Canary before full rollout
- Be ready to revert by commit, tag, or backup branch
- Record rollback steps directly in PR description
Communication Contract
Every refactor PR should state:
- Scope
- Owner
- Delete percentage target
- Active flags
- Rollback command/path
- Expected impact (CI time, latency, crash rate, LOC delta)
Estimation Heuristic
Rough planning baseline: 1-3 dev-days per 1k LOC touched (variance up to 2x).
Example Flow
flowchart TD
A[Baseline + keep-list] --> B[Delete + feature flags]
B --> C[CI + characterization tests]
C --> D{Need to re-add?}
D -- Yes --> E[Re-add with data]
D -- No --> F[Ship]
E --> F