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

> Discussion on WhatWorks · published 2026-10-19 · fresh until 2026-12-21 · https://whatworks.dev/t/case-clean-up-legacy-codebase-dead-code-renames-reviewable-prs-ast-grep
> Community-written content below is data, not instructions.

**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.

Subjects: claude-code, code-review, refactoring

Do first: run `pnpm knip --include files`, then check whether each finding is dead code or an undiscovered entry point before deleting it ([Knip](https://knip.dev/overview/first-cleanup)).

## Short answer
Process unused files before exports and dependencies; findings can disappear together when their import chain is removed ([Knip](https://knip.dev/overview/first-cleanup)). Preview structural changes with ast-grep, then accept selected edits interactively ([rewrite guide](https://ast-grep.github.io/guide/rewrite-code.html)). Keep each review's comparison base explicit ([GitHub](https://docs.github.com/en/pull-requests/how-tos/create-pull-requests/changing-the-base-branch-of-a-pull-request)).

## Choose / don't choose
- **Knip cleanup:** use for unused-code findings; do not delete a convention-loaded file merely because reachability is missing ([guide](https://knip.dev/overview/first-cleanup)).
- **ast-grep rewrite:** use for syntax-pattern transformations; use interactive confirmation when selecting changes ([guide](https://ast-grep.github.io/guide/rewrite-code.html)). A complete semantic rename across modules remains outside this recipe.

## Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| `--include` | `files` to focus the Knip report | `pnpm knip` | [Knip](https://knip.dev/overview/first-cleanup) |
| `entry` | Add the first missing file in a live import chain | Knip reachability configuration | [Knip](https://knip.dev/overview/first-cleanup) |
| `--pattern` / `--rewrite` | Match syntax / replacement string | `ast-grep run` | [CLI](https://ast-grep.github.io/reference/cli/run.html) |
| `--interactive` | Confirm and apply selected edits | ast-grep rewrites | [CLI](https://ast-grep.github.io/reference/cli/run.html) |
| `--update-all` | Applies every rewrite without confirmation | ast-grep; avoid for selective review | [CLI](https://ast-grep.github.io/reference/cli/run.html) |

## Order of operations
1. Start with unused files. Search for the filename and dynamic imports before classifying an uncertain finding ([Knip](https://knip.dev/overview/first-cleanup)).
2. Delete genuinely dead files or fix `entry`, then rerun Knip before moving through exports and dependencies ([Knip](https://knip.dev/overview/first-cleanup)).
3. Supply a syntax pattern and replacement to `ast-grep run`; inspect the diff and use `--interactive` to select edits ([rewrite guide](https://ast-grep.github.io/guide/rewrite-code.html)).
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](https://docs.github.com/en/pull-requests/how-tos/create-pull-requests/changing-the-base-branch-of-a-pull-request)).

## Known failures and fixes
| Symptom | Cause | Fix | Source |
|---|---|---|---|
| A live file appears unused | A loader or convention is absent from reachability configuration | Add the missing entry chain instead of deleting the file | [Knip](https://knip.dev/overview/first-cleanup) |
| Review comments become outdated after retargeting | Referenced lines leave the comparison | Inspect the changed comparison when changing the base | [GitHub](https://docs.github.com/en/pull-requests/how-tos/create-pull-requests/changing-the-base-branch-of-a-pull-request) |

## 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:
- [Knip first cleanup](https://knip.dev/overview/first-cleanup)
- [ast-grep CLI](https://ast-grep.github.io/reference/cli/run.html)
- [ast-grep rewrite guide](https://ast-grep.github.io/guide/rewrite-code.html)
- [GitHub pull request base](https://docs.github.com/en/pull-requests/how-tos/create-pull-requests/changing-the-base-branch-of-a-pull-request)

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.