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.
In this repository8 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- build-graphSkill
- debug-issueSkill
- explore-codebaseSkill
- review-changesSkill
- safe-codeSkill
- safe-refactor-codeSkill
- senior-devSkill
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
- Distinctive: line 12
Treat dead code as code with no live path from any entrypoint, config reference, or runtime hook.
- Very specific: line 14
Prefer Audit first. Use Execute only after the dead-code inventory is clear enough to act safely.
- Distinctive: line 15
If a candidate has dynamic dispatch risk, reflection risk, or external integration risk, flag it instead of deleting it automatically.
- Distinctive: line 16
Never delete generated files.
- Distinctive: line 17
Never mix pruning with unrelated feature work or refactors.
- Distinctive: line 18
Prefer narrow, reversible slices with verification after each slice.
- Distinctive: line 19
Use the codegraph index when available.
- Distinctive: line 23
Use this path before manual scanning when a codegraph index is available:
- Very specific: line 41
No agent detection is needed.
- Very specific: line 43
When flagging candidates that are not auto-deleted, write a note into .safe-code/safe-refactor-code.md so future agents do not rediscover …
- Very specific: line 45
When running under safe-code, follow safe-code's Draft-Until-Save timing: draft these notes (and any AGENTS.md updates) in …
- Distinctive: line 56
Delete high-confidence candidates slice by slice with verification after each slice.
- Distinctive: line 59
Run the same workflow, but scoped to a file, directory, module, or subsystem.
- Distinctive: line 65
Ask a question only if the scope is materially ambiguous.
- Distinctive: line 78
Check these entrypoint classes:
- Distinctive: line 83
test runner entry files when test-only code is relevant
- Distinctive: line 86
If a file is named in config, treat that as a live reference until proven otherwise.
- Distinctive: line 92
When a codegraph index is available, combine its candidates with manual evidence:
- Distinctive: line 106
Do not treat string-built imports, event registries, reflection-heavy systems, or plugin loaders as safe deletion targets unless you can …
- Distinctive: line 129
Search for direct references.
- Distinctive: line 130
Search for indirect references such as re-exports, dynamic names, reflection, generated references, and config mentions.
- Distinctive: line 138
When blast radius is not zero, update callers first and delete only after the updated graph is clean.
- Distinctive: line 142
Use these deletion thresholds:
- Very specific: line 148
Only auto-delete High confidence candidates in Execute mode.
- Distinctive: line 171
Do not combine multiple risky categories into one slice.
- Distinctive: line 175
Before deleting a slice, verify:
- Very specific: line 183
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 …
- Distinctive: line 189
run the narrowest useful verification
- Distinctive: line 191
stop if the slice does not stabilize quickly
- Distinctive: line 193
Prefer checks such as:
- Distinctive: line 201
If verification fails and the issue is not immediately repairable, roll back that slice only and report the blocker.
- Distinctive: line 272
Never delete a file with unresolved dynamic-reference risk without explicit user approval.
- Distinctive: line 273
Never delete generated files.
- Distinctive: line 274
Never delete a module exposed via package exports or public API without verifying consumer risk.
- Distinctive: line 275
Never delete code named by CI, Docker, cron, deploy scripts, or runtime config without checking those references directly.
- Distinctive: line 276
Never continue past a failing verification.
- Distinctive: line 277
Never assume "unused today" means safe to remove.
- Very specific: line 279
No destructive git in a checkout that may be shared: the safe-code skill's references/multi-session.md (Shared-checkout rules).
- Very specific: line 283
If widespread dead code appears beyond the user's requested area, switch to full-repo Audit mode before deleting more.
≈ “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.
Files
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
Auditfirst. UseExecuteonly 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:
codegraph sync(or$build-graphwhen no index exists)- Zero-caller symbols: functions/methods whose
codegraph callers <name>is empty — no function callers AND no file references (exact loop: the safe-code skill'sreferences/graph-integration.md, Dead-code derivation) - Orphan modules:
codegraph node -f <file> --symbols-onlyreports "no other indexed file depends on it" codegraph impact <symbol>before deleting anything sharedcodegraph 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.jsonscripts, 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 referenceDead (confirmed): no live path and no meaningful dynamic-reference riskDead (suspected): no static path found, but dynamic or external-reference risk existsExcluded: 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:
- Search for direct references.
- Search for indirect references such as re-exports, dynamic names, reflection, generated references, and config mentions.
- Count the affected files or systems.
- Classify blast radius:
Zero: nothing references itLocal: same module onlyCross-module: multiple subsystemsExternal: 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 mentionsMedium: zero static references, but dynamic patterns exist nearbyLow: 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:
- commented-out blocks
- orphaned tests for already-deleted modules
- unused exports within a module
- dead functions within a module
- orphaned modules
- stale routes
- legacy adapters
- 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:
- run the narrowest useful verification
- fix one clear follow-up issue if the deletion exposed something small
- 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-RunDry-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.mdExecute 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, neverrm). - 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.
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
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.

