refactor-cleaner
Refactor & Dead Code Cleaner
Identify and remove dead code, duplicates, and unused exports to keep the codebase lean and maintainable. Safety-first approach with comprehensive documentation.
Related Skills:
kavak-documentation- Query for Kavak-specific patterns that might look like dead code but are used dynamically- Use
kavak-platform/platform_docs_searchMCP tool to verify before removing Kavak SDK/platform code
Quick Start
Detect project type and run appropriate analysis:
Node/TypeScript:
npx knip # unused exports/files/deps
npx depcheck # unused dependencies
npx ts-prune # unused exports
Go:
go mod tidy # remove unused deps
deadcode ./... # find unreachable code (golang.org/x/tools)
staticcheck ./... # includes unused code detection
Python:
vulture . # find dead code
pip-autoremove # unused dependencies
Java:
./mvnw dependency:analyze # unused dependencies
# Use IDE or SpotBugs for dead code detection
Workflow
1. Analysis Phase
Run detection tools and categorize findings:
| Risk Level | Examples | Action |
|---|---|---|
| SAFE | Unused exports, unused deps | Remove after grep verify |
| CAREFUL | Dynamic imports possible | Manual review required |
| RISKY | Public API, shared utils | Do not remove |
2. Risk Assessment
For each item to remove:
- Grep for all references (including string patterns)
- Check for dynamic imports
- Verify not part of public API
- Review git history for context
3. Safe Removal Process
a) Start with SAFE items only
b) Remove one category at a time (this order matters — each step
may reveal more dead code in the next category):
1. Unused npm/go dependencies (safest, no code changes)
2. Unused internal exports (removing these may make files unused)
3. Unused files (now visible after export cleanup)
4. Duplicate code (consolidate remaining logic)
c) Run tests after each batch
d) Commit each batch separately
4. Document Deletions
Update docs/DELETION_LOG.md after each session:
## [YYYY-MM-DD] Refactor Session
### Removed
- package-name - Reason
- src/unused-file.ts - Replaced by X
### Impact
- Files: -15, Deps: -5, Lines: -2,300
Safety Rules
Before removing ANYTHING:
- Run detection tools
- Grep for all references
- Check dynamic imports
- Run all tests
- Create backup branch
After each removal:
- Build succeeds
- Tests pass
- Commit changes
- Update DELETION_LOG.md
When NOT to Use
- During active feature development
- Right before production deployment
- Without proper test coverage
- On code you don't understand
Error Recovery
# Immediate rollback if something breaks
git revert HEAD
# Reinstall deps and verify (by language)
# Node: npm install && npm run build && npm test
# Go: go mod download && go build ./... && go test ./...
# Python: pip install -r requirements.txt && pytest
# Java: ./mvnw clean install
Then investigate: Was it a dynamic import/reflection? Update "DO NOT REMOVE" list.
References
| Reference | Purpose |
|---|---|
references/detection-tools.md |
Tool commands and usage |
references/safety-checklist.md |
Detailed safety procedures |
references/deletion-log.md |
Log format and examples |
references/patterns.md |
Common dead code patterns |
references/pr-template.md |
PR template for cleanup |
More from carvalab/k-skills
code-review
Use this skill as the FINAL step after writing or modifying code — reviews for logic bugs, architecture violations, security issues, and performance problems. Trigger after completing a feature, fixing a bug, before merging, or when asked to "review this code", "check my changes", or "is this ready to merge". Fixes issues directly and runs quality gates (lint, typecheck, build, tests). Delegates style to automation, focuses on what matters.
38code-simplifier
Use this skill after writing or modifying code to simplify it — reduces complexity, eliminates redundancy, and improves naming while preserving exact behavior. Trigger after implementing a feature, after a refactor, or when asked to "clean up this code", "simplify this", "make this more readable", or "reduce complexity". Also use when code feels too nested, verbose, or hard to follow. For removing dead code and unused dependencies with detection tools (knip, ts-prune, deadcode), use refactor-cleaner instead.
24backend-development
Use this skill for ANY task in a Node.js/TypeScript or Go backend codebase — adding features, fixing bugs, refactoring, adding flows, modifying handlers, changing business logic, or writing new code. Trigger even when the task is vague like "add the flow to this", "implement this feature", "fix this", or "add X like Y" — if the project has go.mod, nest-cli.json, express routes, or server-side TypeScript, this skill applies. Covers REST APIs, PostgreSQL, Redis, authentication, job queues, events, microservices, Docker, CI/CD, Clean Architecture, SOLID, DRY, and code reuse patterns. When in doubt whether this is backend work, use this skill.
24doc-updater
Use this skill when documentation needs updating — after adding features, changing APIs, modifying architecture, or updating dependencies. Trigger on "update the docs", "generate codemap", "refresh the README", "document this", "update architecture docs", or when code changes make existing documentation stale. Generates codemaps from actual code, updates READMEs, architecture diagrams, and guides.
22frontend-development
Use this skill for ANY task in a Next.js or React frontend codebase — adding pages, building components, fixing UI bugs, styling, handling forms, fetching data, or modifying layouts. Trigger even when the task is vague like "add this feature", "fix the UI", "make this page", or "update the form" — if the project has next.config.*, React components, or client-side TypeScript, this skill applies. Covers App Router, Server Components, Server Actions, MUI styling, Zod validation, caching, and design quality. When in doubt whether this is frontend work, use this skill.
21test-driven-development
Use this skill when writing new features, fixing bugs, or adding test coverage. Enforces Red-Green-Refactor — write the test first, then the code. Trigger on "add tests", "write tests first", "TDD", "test this feature", "fix this bug" (reproduce with a failing test first), or when starting any new implementation. Prevents testing anti-patterns like over-mocking, test-per-method, and tests that pass but verify nothing.
19