agentleFS
afu-itafu-it/safe-code/skills/codebase-pruner/SKILL.md

codebase-pruner

Skill4 starsChanged 57 days agoMITView on GitHub

Scan an entire codebase to detect and safely remove dead code such as unused functions, orphaned modules, unreferenced exports, stale configs, dead routes, and leftover workflow artifacts. Use when asked to clean up dead code, remove unused code, prune stale files, find orphaned modules, audit codebase bloat, or delete code left behind after a workflow or architecture change.

The file

Show its rules and text

We split this skill into the rules it gives an agent

39 rules. About 0 are common, 31 are distinctive, and 8 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈31

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 8

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Treat dead code as code with no live path from any entrypoint, config reference, or runtime hook.

    line 12
  2. Very specific:

    Prefer Audit first. Use Execute only after the dead-code inventory is clear enough to act safely.

    line 14
  3. Distinctive:

    If a candidate has dynamic dispatch risk, reflection risk, or external integration risk, flag it instead of deleting it automatically.

    line 15
  4. Distinctive:

    Never delete generated files.

    line 16
  5. Distinctive:

    Never mix pruning with unrelated feature work or refactors.

    line 17
  6. Distinctive:

    Prefer narrow, reversible slices with verification after each slice.

    line 18
  7. Distinctive:

    Use the codegraph index when available.

    line 19
  8. Distinctive:

    Use this path before manual scanning when a codegraph index is available:

    line 23
  9. Very specific:

    No agent detection is needed.

    line 41
  10. Very specific:

    When flagging candidates that are not auto-deleted, write a note into .safe-code/safe-refactor-code.md so future agents do not rediscover …

    line 43
  11. Very specific:

    When running under safe-code, follow safe-code's Draft-Until-Save timing: draft these notes (and any AGENTS.md updates) in …

    line 45
  12. Distinctive:

    Delete high-confidence candidates slice by slice with verification after each slice.

    line 56
  13. Distinctive:

    Run the same workflow, but scoped to a file, directory, module, or subsystem.

    line 59
  14. Distinctive:

    Ask a question only if the scope is materially ambiguous.

    line 65
  15. Distinctive:

    Check these entrypoint classes:

    line 78
  16. Distinctive:

    test runner entry files when test-only code is relevant

    line 83
  17. Distinctive:

    If a file is named in config, treat that as a live reference until proven otherwise.

    line 86
  18. Distinctive:

    When a codegraph index is available, combine its candidates with manual evidence:

    line 92
  19. Distinctive:

    Do not treat string-built imports, event registries, reflection-heavy systems, or plugin loaders as safe deletion targets unless you can …

    line 106
  20. Distinctive:

    Search for direct references.

    line 129
  21. Distinctive:

    Search for indirect references such as re-exports, dynamic names, reflection, generated references, and config mentions.

    line 130
  22. Distinctive:

    When blast radius is not zero, update callers first and delete only after the updated graph is clean.

    line 138
  23. Distinctive:

    Use these deletion thresholds:

    line 142
  24. Very specific:

    Only auto-delete High confidence candidates in Execute mode.

    line 148
  25. Distinctive:

    Do not combine multiple risky categories into one slice.

    line 171
  26. Distinctive:

    Before deleting a slice, verify:

    line 175
  27. Very specific:

    If the worktree is dirty and deletion would be risky, prefer Audit or Dry-Run unless the user clearly wants execution on top of local …

    line 183
  28. Distinctive:

    run the narrowest useful verification

    line 189
  29. Distinctive:

    stop if the slice does not stabilize quickly

    line 191
  30. Distinctive:

    Prefer checks such as:

    line 193
  31. Distinctive:

    If verification fails and the issue is not immediately repairable, roll back that slice only and report the blocker.

    line 201
  32. Distinctive:

    Never delete a file with unresolved dynamic-reference risk without explicit user approval.

    line 272
  33. Distinctive:

    Never delete generated files.

    line 273
  34. Distinctive:

    Never delete a module exposed via package exports or public API without verifying consumer risk.

    line 274
  35. Distinctive:

    Never delete code named by CI, Docker, cron, deploy scripts, or runtime config without checking those references directly.

    line 275
  36. Distinctive:

    Never continue past a failing verification.

    line 276
  37. Distinctive:

    Never assume "unused today" means safe to remove.

    line 277
  38. Very specific:

    No destructive git in a checkout that may be shared: the safe-code skill's references/multi-session.md (Shared-checkout rules).

    line 279
  39. Very specific:

    If widespread dead code appears beyond the user's requested area, switch to full-repo Audit mode before deleting more.

    line 283

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Codebase Pruner

Audit a repo for dead code, estimate deletion risk, and remove only high-confidence candidates in small verified slices. Reason from real entrypoints, config references, and runtime wiring instead of guessing from filenames.

Core Rules

  • Treat dead code as code with no live path from any entrypoint, config reference, or runtime hook.
  • Map references before deleting anything. One missed reference invalidates the deletion.
  • Prefer Audit first. Use Execute only after the dead-code inventory is clear enough to act safely.
  • If a candidate has dynamic dispatch risk, reflection risk, or external integration risk, flag it instead of deleting it automatically.
  • Never delete generated files. Update the generator or generated output flow instead.
  • Never mix pruning with unrelated feature work or refactors.
  • Prefer narrow, reversible slices with verification after each slice.
  • Use the codegraph index when available. Treat graph output as evidence, not permission to delete.

Graph-Aware Audit Path

Use this path before manual scanning when a codegraph index is available:

  1. codegraph sync (or $build-graph when no index exists)
  2. Zero-caller symbols: functions/methods whose codegraph callers <name> is empty — no function callers AND no file references (exact loop: the safe-code skill's references/graph-integration.md, Dead-code derivation)
  3. Orphan modules: codegraph node -f <file> --symbols-only reports "no other indexed file depends on it"
  4. codegraph impact <symbol> before deleting anything shared
  5. codegraph sync + codegraph affected <changed files> after edits, to pick the tests to run

codegraph has no dead-code command, and callers resolves by name — same-named methods merge, so a dead method can hide behind a live namesake. Confirm every candidate with rg plus a positive control. Public API = the package's entry points (manifest main / exports / bin, a published index, a documented CLI/HTTP surface) — Medium at best; an export keyword inside a private app is not public API. If codegraph is unavailable, empty, or fails, continue with the manual entrypoint and reference graph workflow below. Do not lower deletion confidence because graph data is missing.

Step 0: Locate Doc Folder

Session and continuity docs live in a single agent-agnostic folder at the repo root:

doc folder = <repo-root>/.safe-code/

No agent detection is needed. Codex, Claude, Cursor, and Windsurf all share the same .safe-code/ folder so continuity belongs to the repo, not the tool. Create <repo-root>/.safe-code/ if it does not exist yet.

When flagging candidates that are not auto-deleted, write a note into .safe-code/safe-refactor-code.md so future agents do not rediscover the same uncertain candidates from scratch.

When running under safe-code, follow safe-code's Draft-Until-Save timing: draft these notes (and any AGENTS.md updates) in .safe-code/SESSION.md and let /safe-code --save apply them. Direct writes to safe-refactor-code.md and AGENTS.md are standalone behavior only.

Modes

Audit

Scan and report only. Produce a prioritized dead-code inventory with confidence and blast radius. This is the default mode.

Dry-Run

Produce a deletion plan with ordered slices, rollback points, and verification commands. Do not edit files.

Execute

Delete high-confidence candidates slice by slice with verification after each slice. Stop on failure.

Targeted

Run the same workflow, but scoped to a file, directory, module, or subsystem.

Workflow

1. Clarify Scope Only When Needed

Ask a question only if the scope is materially ambiguous. Otherwise discover the answers locally.

Resolve these questions if they matter:

  • full repo or targeted area
  • whether intentionally-unused compatibility code exists
  • whether tests, fixtures, generated files, and build artifacts are in scope
  • whether config files such as CI, Docker, cron, and env wiring are in scope

2. Map Live Entrypoints First

Build the list of roots before scanning for dead code.

Check these entrypoint classes:

  • application entry files such as main, server, index, or CLI entry scripts
  • framework-loaded files such as routes, middleware, plugins, and hooks
  • package.json scripts, package exports, and bin entries
  • test runner entry files when test-only code is relevant
  • config-driven entrypoints from Dockerfiles, compose files, CI workflows, cron jobs, reverse proxies, and deploy scripts

If a file is named in config, treat that as a live reference until proven otherwise.

3. Build the Reference Graph

Traverse imports, requires, includes, re-exports, and config references from every entrypoint.

When a codegraph index is available, combine its candidates with manual evidence:

  • Graph "no callers" plus no config or dynamic references -> possible High confidence.
  • Graph "no callers" but dynamic dispatch, registry, route loader, reflection, package export, or config mention -> Medium or Low.
  • Manual references found outside graph -> not dead until reconciled.
  • Graph impact radius > 10 files -> stop and report before deletion.

For each candidate, classify it as:

  • Live: reachable from an entrypoint or config reference
  • Dead (confirmed): no live path and no meaningful dynamic-reference risk
  • Dead (suspected): no static path found, but dynamic or external-reference risk exists
  • Excluded: generated files, vendored dependencies, build output, or other out-of-scope material

Do not treat string-built imports, event registries, reflection-heavy systems, or plugin loaders as safe deletion targets unless you can prove they are unused.

4. Classify Candidate Type

Common categories:

  • orphaned modules
  • unused exports
  • dead functions
  • stale routes
  • legacy adapters
  • commented-out blocks
  • stale configs
  • orphaned tests
  • abandoned feature flags
  • dead env vars

This helps order the work from safest to riskiest.

5. Estimate Blast Radius

For every candidate:

  1. Search for direct references.
  2. Search for indirect references such as re-exports, dynamic names, reflection, generated references, and config mentions.
  3. Count the affected files or systems.
  4. Classify blast radius:
    • Zero: nothing references it
    • Local: same module only
    • Cross-module: multiple subsystems
    • External: referenced by config, CI, Docker, or outside consumers

When blast radius is not zero, update callers first and delete only after the updated graph is clean.

6. Score Confidence

Use these deletion thresholds:

  • High: zero references, no dynamic risk, no config mentions
  • Medium: zero static references, but dynamic patterns exist nearby
  • Low: only weak evidence such as string mentions, docs, or naming collisions

Only auto-delete High confidence candidates in Execute mode. Flag Medium and Low unless the user explicitly asks for a riskier pass.

7. Plan Slices

Express removals as small, ordered slices. Each slice must include:

  • narrow goal
  • touched files
  • preserved invariant
  • verification command
  • rollback action (from the slice's own backup, or git show HEAD:<path> > <path> for a file that was clean before the slice)

Preferred order:

  1. commented-out blocks
  2. orphaned tests for already-deleted modules
  3. unused exports within a module
  4. dead functions within a module
  5. orphaned modules
  6. stale routes
  7. legacy adapters
  8. stale configs

Do not combine multiple risky categories into one slice.

8. Run the Pre-Delete Check

Before deleting a slice, verify:

  • the worktree state is understood: each path in the slice is clean (git status --porcelain -- <path> empty) or was changed only by this task — never delete or restore over another session's uncommitted edits
  • the candidate was scanned against the full relevant graph
  • codegraph callers/dependents/impact were checked when an index is available
  • generated references were refreshed if needed
  • the candidate is not part of a package export, plugin registry, deploy script, or runtime registration path

If the worktree is dirty and deletion would be risky, prefer Audit or Dry-Run unless the user clearly wants execution on top of local changes.

9. Execute With Verification

After each slice:

  1. run the narrowest useful verification
  2. fix one clear follow-up issue if the deletion exposed something small
  3. stop if the slice does not stabilize quickly

Prefer checks such as:

  • lint
  • type-check
  • targeted tests
  • focused build or compile probe
  • targeted runtime/manual probe when automation is absent

If verification fails and the issue is not immediately repairable, roll back that slice only and report the blocker.

10. Summarize Clearly

Return:

  • doc folder used
  • files scanned and entrypoints mapped
  • candidates found by category and confidence
  • removals completed
  • flagged candidates not removed and why
  • verification run per slice
  • remaining risks or dynamic-reference uncertainties

Output Shapes

Audit Output

Doc folder: .safe-code/
Entrypoints mapped: 7
Files scanned: 184

Dead code inventory:
[HIGH] Orphaned module: src/adapters/old-client.ts - no live imports or config references
[HIGH] Dead function: src/utils/date.ts:legacyFormatDate - 0 callers
[MEDIUM] Suspected dead: src/handlers/webhook-v1.ts:handleEvent - dynamic dispatch risk
[LOW] Stale env var: OLD_REDIS_URL - not read in scanned files

Total confirmed dead: 2
Total suspected dead: 2
Recommended next step: Dry-Run

Dry-Run Output

Removal plan:
Slice 1 - Remove commented-out blocks in src/utils/date.ts
  Files: src/utils/date.ts
  Invariant: no behavior change
  Verification: npm run lint
  Rollback: restore touched file from local backup or git show HEAD:<path>

Slice 2 - Remove orphaned module src/adapters/old-client.ts
  Files: src/adapters/old-client.ts
  Invariant: no live caller exists
  Verification: import scan + npm run lint
  Rollback: restore touched file from local backup or git show HEAD:<path>

Flagged for manual review:
  src/handlers/webhook-v1.ts:handleEvent - dynamic dispatch risk
  → noted in .safe-code/safe-refactor-code.md

Execute Output

Slice 1 complete: removed commented-out blocks in src/utils/date.ts
  Verification: lint passed

Slice 2 complete: removed src/adapters/old-client.ts
  Verification: lint passed, import scan clean

Summary:
  Removed: 1 file, 1 function, 5 commented blocks
  Flagged: 1 candidate (noted in .safe-code/safe-refactor-code.md)

Hard Rules

  • Never delete a file with unresolved dynamic-reference risk without explicit user approval.
  • Never delete generated files.
  • Never delete a module exposed via package exports or public API without verifying consumer risk.
  • Never delete code named by CI, Docker, cron, deploy scripts, or runtime config without checking those references directly.
  • Never continue past a failing verification.
  • Never assume "unused today" means safe to remove; check runtime wiring and recent intent where relevant.
  • Deleting files: follow the safe-code skill's Safety Invariants (one shell command, one purpose; tracked -> git rm, untracked -> backups or trash, never rm).
  • No destructive git in a checkout that may be shared: the safe-code skill's references/multi-session.md (Shared-checkout rules).

Escalation

If widespread dead code appears beyond the user's requested area, switch to full-repo Audit mode before deleting more. Always note flagged but unremoved candidates in .safe-code/safe-refactor-code.md so future agents do not rediscover the same uncertain candidates from scratch.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.