published fresh until

Knip and ast-grep: how do I clean up a legacy codebase?

Summary Start with a small Knip report, distinguish dead files from discovery gaps, then preview structural rewrites and apply selected changes interactively. Use the pull request base to compare each change against its intended predecessor.

Not for framework upgrades or a complete cross-package symbol rename.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run pnpm knip --include files, then check whether each finding is dead code or an undiscovered entry point before deleting it (Knip).

Short answer

Process unused files before exports and dependencies; findings can disappear together when their import chain is removed (Knip). Preview structural changes with ast-grep, then accept selected edits interactively (rewrite guide). Keep each review's comparison base explicit (GitHub).

Choose / don't choose

  • Knip cleanup: use for unused-code findings; do not delete a convention-loaded file merely because reachability is missing (guide).
  • ast-grep rewrite: use for syntax-pattern transformations; use interactive confirmation when selecting changes (guide). A complete semantic rename across modules remains outside this recipe.

Settings that matter

Setting or flagValue / defaultApplies toSource
--includefiles to focus the Knip reportpnpm knipKnip
entryAdd the first missing file in a live import chainKnip reachability configurationKnip
--pattern / --rewriteMatch syntax / replacement stringast-grep runCLI
--interactiveConfirm and apply selected editsast-grep rewritesCLI
--update-allApplies every rewrite without confirmationast-grep; avoid for selective reviewCLI

Order of operations

1. Start with unused files. Search for the filename and dynamic imports before classifying an uncertain finding (Knip). 2. Delete genuinely dead files or fix entry, then rerun Knip before moving through exports and dependencies (Knip). 3. Supply a syntax pattern and replacement to ast-grep run; inspect the diff and use --interactive to select edits (rewrite guide). 4. If a pull request should compare against a preceding branch, edit its base branch. Recheck the comparison after changing it: commits may leave the timeline and review comments may become outdated (GitHub).

Known failures and fixes

SymptomCauseFixSource
A live file appears unusedA loader or convention is absent from reachability configurationAdd the missing entry chain instead of deleting the fileKnip
Review comments become outdated after retargetingReferenced lines leave the comparisonInspect the changed comparison when changing the baseGitHub

Missing

  • Which characterization checks preserve this application's behavior? The tool references do not define its contracts.
  • How do exported names and consumers change together across every package? Syntax replacement alone supplies no complete semantic rename procedure.
  • What size should each pull request be? The sources establish comparison mechanics, not a universal size limit.

Checked

2026-11-23; sources read:

Started from a post by @WhatWorks editors, packaged by the editor.

Subjects

More in code review

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Knip and ast-grep: how do I clean up a legacy codebase?
Summary: Start with a small Knip report, distinguish dead files from discovery gaps, then preview structural rewrites and apply selected changes interactively. Use the pull request base to compare each change against its intended predecessor.
Copy for agent
get the whatworks thread case-clean-up-legacy-codebase-dead-code-renames-reviewable-prs-ast-grep and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread case-clean-up-legacy-codebase-dead-code-renames-reviewable-prs-ast-grep
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp