Cleanup unused

From NVlabs/Skill2Env · View interactive version

Cleanup unused is an AI agent skill from the GitHub repository NVlabs/Skill2Env. Detect and delete unused code, exports, files, and dependencies. Runs knip/vulture/staticcheck/cargo-machete appropriate to the language, writes a critical assessment, and auto-applies HIGH-confidence deletions. Use when the user asks to remove dead code, find unused exports, clean up dependencies,

How to use this skill

Skill file

Detect unused code, exports, files, and dependencies. Auto-delete only what's verifiably dead. Write a critical assessment with confidence ratings; defer ambiguous items to the human.

Preflight

  1. Detect language(s) by checking for: package.json (TS/JS), pyproject.toml/requirements.txt (Py), go.mod (Go), Cargo.toml (Rust). Skip languages not present.
  2. Check git state. If working tree is dirty, refuse to auto-apply — ask user to stash or commit first. The verify step needs a clean baseline to revert to.
  3. Create report dir: mkdir -p .claude/cleanup-reports/. Add .claude/cleanup-reports/ to .gitignore if not already present.
  4. Scan for dynamic-import patterns that defeat static analysis. Save the list — findings in these areas drop to MEDIUM confidence.

Detect

Run the right tool per language. Install on demand only after asking.

TypeScript / JavaScript

# Knip is the gold standard. Most TS/JS repos already have it wired up.
bunx knip --reporter json > /tmp/knip.json 2>/dev/null || npx knip --reporter json > /tmp/knip.json

Parse files, exports, types, dependencies, devDependencies, unlisted, binaries arrays.

Python

uvx vulture . --min-confidence 80 --json > /tmp/vulture.json 2>/dev/null || pipx run vulture . --min-confidence 80

Vulture confidence ≥80 maps roughly to our HIGH; 60-79 = MEDIUM.

Go

go install honnef.co/go/tools/cmd/staticcheck@latest 2>/dev/null
staticcheck -checks=U1000 ./... > /tmp/staticcheck.txt

U1000 = unused code.

Rust

cargo install cargo-machete 2>/dev/null
cargo machete --with-metadata > /tmp/machete.txt    # unused deps
cargo +nightly udeps 2>/dev/null || true            # optional, nightly-only

Use RUSTFLAGS="-W dead_code" and re-build for in-crate dead code if needed.

Assess

Write .claude/cleanup-reports/cleanup-unused-{YYYY-MM-DD}.md with:

# Unused Code Assessment — YYYY-MM-DD

## Scope
- Languages detected: [TS, Py, …]
- Tools run: [knip, vulture, …]
- Dynamic-import risk areas: [list paths]

## Summary
- Unused files: N (HIGH: x, MEDIUM: y)
- Unused exports: N (HIGH: x, MEDIUM: y)
- Unused dependencies: N (HIGH: x, MEDIUM: y)
- Total LOC removable (HIGH only): ~N

## Findings

| Conf | Type | Location | Tool said | Recommendation |
|------|------|----------|-----------|----------------|
| HIGH | export | src/utils/foo.ts:12 `parseDate` | knip: unused export | Delete export + function |
| MED  | file | src/legacy/old.ts | knip: unused file | Defer — referenced from dynamic import area |
| ...

## Critical Assessment

[2-4 paragraphs of human-readable analysis]
- What patterns emerged? (e.g., "8 of 12 unused exports are in `lib/legacy/` — consider deleting the whole directory")
- Why were items downgraded from HIGH to MEDIUM?
- Architectural observations.

## Out of scope
- [Items the tool flagged but skill won't touch — public API surfaces, framework conventions, etc.]

Apply

Auto-apply HIGH-confidence only. Group all deletions into one commit.

Confidence rubric

HIGH (auto-apply):

MEDIUM (report only, don't apply):

LOW (note in report, no action):

Execution

  1. Delete files: rm <path> — but verify no .gitignore'd sibling files reference them first.
  2. Remove exports: Edit to remove the export keyword + drop the symbol if internal usage is also zero.
  3. Uninstall deps: bun remove <pkg> / pip uninstall <pkg> / cargo remove <pkg> — pick the manager from lockfile presence.
  4. Single commit: git add -A && git commit -m "chore(cleanup): cleanup-unused — N items removed".

Verify

Run all that apply, in order. Halt and revert on first failure.

# Typecheck
bun run typecheck 2>&1 || npx tsc --noEmit
mypy . 2>&1 || true
go build ./... 2>&1
cargo check 2>&1

# Tests (if scripts exist)
bun test 2>&1 || npm test 2>&1
pytest 2>&1
go test ./... 2>&1
cargo test 2>&1

# Lint
bunx biome check . 2>&1 || npx eslint . 2>&1
ruff check . 2>&1
golangci-lint run 2>&1
cargo clippy 2>&1

If any fail: git revert HEAD --no-edit, downgrade all auto-applied items to MEDIUM in the report, append a "## Verify Failure" section with the error output.

Output

End-of-turn message (≤4 lines):

NEVER

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.