{"data":{"slug":"discovered-a50f4182be9ce20bcd617c84726d2ad2f272fe6fb554f754","title":"Dead Code Hygiene","summary":"Remove unused legacy or inert code after replacements and refactors to prevent confusion in later agent context. This includes deleting old files, updating imports, and ensuring no public APIs are removed without confirmation.","url":"https:\/\/skillshunter.net\/skills\/discovered-a50f4182be9ce20bcd617c84726d2ad2f272fe6fb554f754","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/discovered-a50f4182be9ce20bcd617c84726d2ad2f272fe6fb554f754","markdown_url":"https:\/\/skillshunter.net\/skills\/discovered-a50f4182be9ce20bcd617c84726d2ad2f272fe6fb554f754.md","download_url":"https:\/\/skillshunter.net\/skills\/discovered-a50f4182be9ce20bcd617c84726d2ad2f272fe6fb554f754\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before deletion, search for references using grep or linters, run typecheck and tests, and ensure no dynamic or config-based references remain. When uncertain, add a `TODO(dead-code): verify callers` note instead of deleting.","instructions":"---\nname: dead-code-hygiene\nversion: 1.0.0\ndescription: Remove unused legacy\/inert code after replacements and refactors so leftover paths do not confuse later agent context. Invoke after you replace a module, dual-path, shim, or helper and before quality gates. Prefer evidence (grep + typecheck\/tests) over guesswork; never delete public APIs without confirmation.\n---\n\n# Dead-Code Hygiene\n\n**Invoke after a replacement, migration, or refactor** that makes an old path unreachable \u2014 before quality gates and commit. Leftover inert code is read by the next session and steers agents the wrong way.\n\n> Prefer **delete in the same change** over \u201cleave for later.\u201d Commenting-out or `\/\/ LEGACY` stubs are still context pollution.\n\n---\n\n## Golden Rules\n\n1. **Same change, not a follow-up TODO.** When you introduce the new path, remove the old one in that edit set.\n2. **Evidence before delete.** Grep for callers\/imports\/route names; run typecheck\/tests. No evidence \u2192 leave it and note uncertainty.\n3. **Scope tight on `\/fix`.** Only remove code made unreachable by *this* fix (old branch, dead shim). Broad cleanup \u2192 separate `refactor:` commit.\n4. **When uncertain, do not delete.** Prefer a short `TODO(dead-code): verify callers` over a wrong deletion.\n5. **Update docs\/domains** when a path you remove was documented.\n\n---\n\n## When to Invoke\n\n| Signal | Action |\n|---|---|\n| Replaced helper \/ module \/ page with a new one | Delete old file + update imports |\n| Dual path (`v1` + `v2`, feature-flag both always-on) | Collapse to the live path; remove dead branch |\n| Compatibility shim with zero remaining callers | Delete shim |\n| Copy-pasted \u201ckeep for rollback\u201d block in same PR | Delete; git history is the rollback |\n| Unused import \/ export flagged by lint | Remove in the same pass |\n\n---\n\n## Verification Protocol (before delete)\n\n1. **Search references** \u2014 symbol name, file basename, route\/component string (Inertia `component:`, Next route segment, Laravel route name).\n2. **Check dynamic uses** \u2014 string dispatch, `import()`, reflection, config maps, Blade\/React name tables.\n3. **Run the stack gate** \u2014 typecheck \/ PHPStan \/ ruff after removal.\n4. **Run relevant tests** \u2014 at least the suite covering the changed surface.\n\n```bash\n# Node \/ TS \u2014 find remaining references (adjust pattern)\nrg -n \"OldHelper|old-helper\" --glob '!dist\/**' --glob '!node_modules\/**'\n\n# PHP\nrg -n \"OldHelper|old_helper\" app\/ routes\/ resources\/ tests\/\n\n# Python\nrg -n \"old_helper|OldHelper\" --glob '!**\/__pycache__\/**' --glob '!.venv\/**'\n```\n\nOptional tooling (prefer over guesswork when installed):\n\n| Stack | Tool |\n|---|---|\n| Node\/TS | ESLint `no-unused-vars` \/ `knip` \/ `ts-prune` |\n| PHP | PHPStan level 4+ (dead code \/ always-true) |\n| Python | Ruff `F401`\/`F841`, `vulture` |\n\n---\n\n## Safe vs Unsafe Deletions\n\n| Safe (with evidence) | Unsafe \u2014 do NOT delete without confirmation |\n|---|---|\n| Unreferenced private helper in same package | Public package API \/ exported library surface |\n| Orphan page after route rewrite | Feature-flagged path still toggled in prod |\n| Dead import after move | Dynamic import \/ string-dispatch target |\n| Empty wrapper that only re-exports removed code | Test fixtures \/ factories used by name |\n| Commented-out blocks left from this session | Migration \u201cdown\u201d paths still required |\n\n---\n\n## Anti-Patterns (FORBIDDEN)\n\n| Action | Why |\n|---|---|\n| Leave `\/\/ OLD:` \/ `# LEGACY` blocks \u201cjust in case\u201d | Next agent reads them as live guidance |\n| Keep dual implementations indefinitely | Doubles maintenance + context noise |\n| Delete by filename alone without grep | Breaks dynamic \/ config references |\n| Broad cleanup inside a minimal `\/fix` | Violates fix scope; mix bugs with refactors |\n| Suppress unused-lint instead of removing | Hides the signal |\n\n---\n\n## Polyglot Examples\n\n```ts\n\/\/ WRONG \u2014 new path + inert old export left for \"rollback\"\nexport { NewCheckout } from '.\/new-checkout';\nexport { OldCheckout } from '.\/old-checkout'; \/\/ unused; confuses next session\n\n\/\/ CORRECT \u2014 single live path\nexport { NewCheckout } from '.\/new-checkout';\n\/\/ delete old-checkout.ts after rg shows zero callers\n```\n\n```php\n\/\/ WRONG \u2014 route still points at unused controller after rewrite\n\/\/ Route::get('\/billing', [OldBillingController::class, 'show']);\n\n\/\/ CORRECT \u2014 update route + delete OldBillingController once rg is clean\nRoute::get('\/billing', [BillingController::class, 'show']);\n```\n\n```python\n# WRONG \u2014 keep unused helper after extract\ndef legacy_normalize(x):  # no callers\n    return x.strip()\n\n# CORRECT \u2014 delete legacy_normalize; keep only the live path\ndef normalize(x: str) -> str:\n    return x.strip()\n```\n\n---\n\n## Pre-Commit Checklist\n\n- [ ] Replaced paths: old files\/exports removed or still have proven callers\n- [ ] No `LEGACY` \/ commented dual implementations added in this change\n- [ ] Grep clean for removed symbol names\n- [ ] Typecheck \/ lint \/ relevant tests pass\n- [ ] Domain docs \/ CLAUDE.md updated if a documented path was removed\n- [ ] `\/fix` scope: only fix-induced dead code (else separate refactor commit)\n\n## See Also\n\n- `final-check` \u2014 mechanical leftovers (`console.log`, `any`, debug dumps); complementary\n- `quality-gate` \u2014 typecheck\/lint\/test\/build after hygiene\n- `git-workflow` \u2014 use `refactor:` for broad dead-code deletions; keep `\/fix` minimal\n- `tool-resilience` \u2014 if grep\/edit tools fail while verifying callers\n- `error-handling` \u2014 application error paths (not inert-code cleanup)\n","use_cases":["Deleting old files after replacing a module or helper","Removing dead branches in dual-path implementations","Cleaning up compatibility shims with no remaining callers","Removing unused imports flagged by linters","Updating documentation when removing documented paths"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/0x57-1\/start-vibing-stacks\/blob\/6e398b34c4dfe73ac8f809216b218856b8e02ff4\/stacks\/_shared\/skills\/dead-code-hygiene\/SKILL.md","source_commit":"6e398b34c4dfe73ac8f809216b218856b8e02ff4","license":null,"version":null,"listing_approved_at":"2026-12-03","created_at":"2026-12-02T09:11:03.000000Z","updated_at":"2026-12-03T16:20:08.000000Z","seo_title":"Dead Code Hygiene - Remove Unused Legacy Code","seo_description":"Ensure clean codebases by removing unused legacy or inert code after replacements and refactors. Follow evidence-based practices to avoid deleting public APIs or leaving\u60f0","seo_keywords":["dead code removal","code cleanup","legacy code hygiene","software refactoring","code maintenance","code hygiene practices"]}}