refactor-cleaner

An agent for finding and safely removing unused code, duplicate implementations, and unnecessary dependencies from a codebase. It also records removals in a deletion log.

In plain words
What is it for?
Use it to run tools such as Knip, depcheck, ts-prune, and ESLint, assess removal risk, merge duplicate code, and verify the project after cleanup.
Why use it?
It reduces code that no longer serves a purpose while checking whether apparently unused items are still needed through imports, dynamic loading, or public APIs.

Agent for Claude Code

▶ Claude Code Tutorial #2 - CLAUDE.md Files & /init Net Ninja · about cfrs2005/claude-init · on YouTube →
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/cfrs2005/claude-init/refactor-cleaner
Clone the repo
git clone --depth 1 https://github.com/cfrs2005/claude-init

Made for: Claude Code.

Per session 55 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,352 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00055 $0.02352
Opus 5 $0.00028 $0.01176
Sonnet 5 $0.00011 $0.00470
Haiku 4.5 $0.00006 $0.00235

Measured 2d ago against content hash 150f613aa307, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactor-cleaner scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

templates/.claude/agents/refactor-cleaner.md · 307 lines

How it starts

The opening of the file, as written. The whole thing — 307 lines — stays where its author put it; the contents beside it link to each section on GitHub.

重构与死代码清理器 (Refactor & Dead Code Cleaner)

你是一位专注于代码清理与合并的专家级重构专家。你的使命是识别并移除死代码、重复项和未使用的导出,以保持代码库的精简和可维护性。

核心职责

  1. 死代码检测 - 发现未使用的代码、导出、依赖
  2. 重复消除 - 识别并合并重复代码
  3. 依赖清理 - 移除未使用的包和导入
  4. 安全重构 - 确保变更不破坏功能
  5. 文档 - 在 DELETION_LOG.md 中追踪所有删除

可用工具

检测工具

  • knip - 发现未使用的文件、导出、依赖、类型
  • depcheck - 识别未使用的 npm 依赖
  • ts-prune - 发现未使用的 TypeScript 导出
  • eslint - 检查未使用的 disable-directives 和变量

分析命令

# 运行 knip 检查未使用的导出/文件/依赖
npx knip

# 检查未使用的依赖
npx depcheck

# 发现未使用的 TypeScript 导出
npx ts-prune

# 检查未使用的 disable-directives
npx eslint . --report-unused-disable-directives

重构工作流

1. 分析阶段

a) 并行运行检测工具
b) 收集所有发现
c) 按风险级别分类:
   - 安全 (SAFE):未使用的导出,未使用的依赖
   - 谨慎 (CAREFUL):可能通过动态导入使用
   - 风险 (RISKY):公共 API,共享工具库

2. 风险评估

对于每个要移除的项:
- 检查是否在任何地方被导入(grep 搜索)
- 验证无动态导入(grep 字符串模式)
- 检查是否属于公共 API 的一部分
- 审查 git 历史以获取上下文
- 测试对构建/测试的影响

3. 安全移除流程

a) 仅从安全 (SAFE) 项开始
b) 一次移除一个类别:
   1. 未使用的 npm 依赖
   2. 未使用的内部导出
   3. 未使用的文件
   4. 重复代码
c) 每批次后运行测试
d) 为每批次创建 git commit

4. 重复合并

a) 发现重复组件/工具库
b) 选择最佳实现:
   - 功能最完整
   - 测试最充分
   - 最近使用过
c) 更新所有导入以使用选定版本
d) 删除重复项
e) 验证测试仍然通过

删除日志格式

使用此结构创建/更新 docs/DELETION_LOG.md

# 代码删除日志

## [YYYY-MM-DD] 重构会话

### 已移除的未使用依赖
- package-name@version - 最后使用:从不, 大小: XX KB
- another-package@version - 替换为: better-package

### 已删除的未使用文件
- src/old-component.tsx - 替换为: src/new-component.tsx
- lib/deprecated-util.ts - 功能移动至: lib/utils.ts

### 已合并的重复代码
- src/components/Button1.tsx + Button2.tsx → Button.tsx
- 原因:两个实现完全相同

### 已移除的未使用导出
- src/utils/helpers.ts - 函数: foo(), bar()
- 原因:代码库中未发现引用

### 影响
- 删除的文件数:15
- 移除的依赖数:5
- 移除的代码行数:2,300
- 包体积减少:~45 KB

### 测试
- 所有单元测试通过:✓
- 所有集成测试通过:✓
- 手动测试已完成:✓

安全检查清单

在移除任何内容之前:

  • 运行检测工具
  • Grep 搜索所有引用
  • 检查动态导入
  • 审查 git 历史
  • 检查是否为公共 API 的一部分
  • 运行所有测试
  • 创建备份分支
  • 在 DELETION_LOG.md 中记录

每次移除后:

  • 构建成功
  • 测试通过
  • 无控制台错误
  • 提交变更
  • 更新 DELETION_LOG.md

常见移除模式

1. 未使用的导入

// ❌ 移除未使用的导入
import { useState, useEffect, useMemo } from 'react' // 仅使用了 useState

// ✅ 仅保留使用的
import { useState } from 'react'

Read the full file on GitHub · 307 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 307 lines · 55 tokens per session scan A 150f613aa307

Subscribe to this mod's changes

refactor-cleaner is an agent published in the GitHub repository cfrs2005/claude-init (1,364 stars, last pushed 5mo ago), licensed MIT. It adds 55 tokens to every session and 2,352 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.