Communityコーディング&開発github.com

remove-dead-code

Delete code that is never executed, never referenced, or no longer reachable. Use when you find unused functions, variables, imports, parameters, branches, or files; when obsolete compatibility paths are no longer needed; or when dead code obscures behavior. Scans the target file, proposes safe deletions, and edits only after approval.

remove-dead-code とは?

remove-dead-code is a Claude Code agent skill that delete code that is never executed, never referenced, or no longer reachable. Use when you find unused functions, variables, imports, parameters, branches, or files; when obsolete compatibility paths are no longer needed; or when dead code obscures behavior. Scans the target file, proposes safe deletions, and edits only after approval.

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Bouzidi-Youssef/refactor-skill/tree/HEAD/.opencode/skills/remove_dead_code

Installed? Explore more コーディング&開発 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Remove Dead Code

Category G — Hygiene and cleanup

Delete code that has no effect on observable behavior because it cannot be reached or is no longer used.

Dead code is liability: it increases reading cost, misleads future changes, and creates false confidence that behavior matters when it does not.


Motivation

The smell

  • Unused symbols. Functions, methods, classes, variables, imports, constants, or files with no references.
  • Unreachable branches. Conditions that can never be true, returns after returns, or handlers for impossible states.
  • Obsolete parameters. Parameters accepted but never used, especially after behavior changes.
  • Commented-out or legacy remnants. Old code kept "just in case" when version control already preserves history.
  • Dead feature flags/compatibility paths. Paths no longer reachable in supported configurations.

Fowler's bias: when you are sure code is not used, delete it; version control can bring it back if needed.

The design principle

  • Less code, less confusion. Readers should not spend effort understanding behavior that never happens.
  • Honest codebase. The remaining code should represent real behavior and real contracts.
  • Safe deletion. Removal is behavior-preserving only when the code is truly dead in all relevant contexts.
  • Behavior preservation. Deleting truly dead code does not change observable behavior; deleting "apparently dead" code can.

Remove Dead Code is not:

  • deleting code you do not understand yet;
  • removing public API used by external consumers;
  • hiding uncertainty by deleting instead of investigating.

When not to remove

  • It may be used dynamically. Reflection, plugin loading, string-based dispatch, serialization, dependency injection, templates, or framework conventions.
  • It is a public/external contract. Exported API, CLI flags, config keys, wire formats, or library entry points may have out-of-repo callers.
  • It is required by tests/build/runtime conventions not visible from a simple reference search.
  • It is intentionally unreachable for safety (e.g., exhaustive-check guard) and conveys meaning; consider whether it is truly dead.
  • You are not sure. Investigate first. Ask before deleting ambiguous cases.

Mechanic

Before you start

  1. Read the file and identify the suspected dead code. Distinguish "unused locally" from "unused everywhere relevant".
  2. Search for references: calls, imports, exports, string references, config, tests, docs, reflection/framework conventions.
  3. Find existing checks. Use tests/type checker/linter/build as available. Dead-code removal often benefits from static analysis, but do not assume it is complete.
  4. Work in small deletions. One dead symbol/path at a time is easier to verify and revert.

Steps

  1. Pick one candidate. Prefer the clearest local dead code: unused local variable, unused private helper, unreachable branch after obvious return.
  2. Prove or qualify deadness. Gather evidence. If evidence is incomplete, mark risk and ask.
  3. Delete the code. Remove the symbol and its now-dead support code: unused imports, unused helpers, stale comments, dead parameters if safe.
  4. Run checks. Use existing tests/type checks/lint/build. If anything fails, determine whether the code was not dead.
  5. Repeat in dependency order. Removing one dead function may make imports or helpers dead; clean them up in follow-up steps.
  6. Report clearly. State what was removed, what evidence supported it, and any remaining uncertain candidates.

Assessing deadness

CandidateEvidence to look forRisk
Unused local variableNo reads after assignment; no side-effect dependencyLow
Unused private function/methodNo references in codebase; not reflected/dynamically calledLow-medium
Unused importSymbol not referenced; no side-effect import neededLow
Unused parameterNo reads; not required by interface/override/external signatureMedium
Unreachable branchProvably impossible condition or control flowMedium
Unused exported/public symbolNo in-repo refs, but may have external consumersHigh
Dead file/moduleNo imports/references; check build/config/entry pointsHigh

Rules that keep the move safe:

  • Do not delete code based only on a gut feeling.
  • Preserve side effects. An "unused" value may come from a call that must still happen.
  • Check interfaces/overrides before removing parameters or methods.
  • Check external/public contracts before removing exports.
  • Prefer small, reversible deletions.

Example

Before

def build_greeting(name: str, formal: bool) -> str:
    display_name = name.strip().title()
    if formal:
        return f"Good day, {display_name}."
    return f"Hello, {display_name}!"


def build_farewell(name: str) -> str:
    display_name = name.strip().title()
    return f"Goodbye, {display_name}."

If build_farewell has no references, tests, exports, or dynamic use, it is dead code.

After

def build_greeting(name: str, formal: bool) -> str:
    display_name = name.strip().title()
    if formal:
        return f"Good day, {display_name}."
    return f"Hello, {display_name}!"

The observable behavior of the used code is unchanged. The codebase is smaller and more honest.

A non-example

def plugin_hook(event):
    return handle(event)

Even with no in-repo callers, a plugin hook may be loaded by name. Do not remove it without confirming the contract.


Related refactorings

  • Inline Function — can leave behind an unreferenced function that becomes dead code.
  • Move Function / Move Field — old locations may become dead after migration.
  • Substitute Algorithm — replacing an algorithm can make old helpers dead.
  • Extract Function — sometimes reveals dead branches or unused values; remove them only when proven dead.

Running this skill on a file

When invoked with @remove_dead_code:

  1. Confirm the target. Use the file named by the user. If none is named, ask for one. Read it completely.
  2. Inventory candidates. For each candidate, report:
    • symbol/location,
    • kind of dead code (unused function, variable, import, parameter, unreachable branch, dead file),
    • evidence for deadness (reference search, call graph, type/lint signal),
    • contract/dynamic-use risk,
    • risk: low (local/private, strong evidence), medium (internal but indirect), high (public/exported/dynamic/serialized).
  3. Propose before editing. Present deletions in safest-first order. Wait for approval. Do not change files on the first pass.
  4. Apply one deletion at a time. Remove the dead code and any clearly dead support code it leaves behind.
  5. Run existing checks after each step. If checks fail, treat it as evidence the code may not have been dead; revert and report.
  6. Report. Say what was removed, evidence used, what was not verified, and any suspicious candidates left untouched.
  7. Stop and ask when deadness cannot be proven, when public/dynamic use is plausible, or when removal would change behavior.

Checklist

  • Deadness is supported by evidence, not guesswork.
  • Side effects were preserved; only truly dead code was removed.
  • Public/dynamic/contract risks were considered.
  • Deletions were small and verified.
  • Checks pass, or the unverified risk is stated.

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

extract-class

Split a class or module that has multiple responsibilities into smaller cohesive units. Use when a class has distinct clusters of fields and methods that change together, when a subset of behavior belongs to a separate concept, or when a class is hard to name because it does too much. Scans the target file, proposes extraction boundaries, and edits only after approval.

extract-function

Turn a coherent block of code into a well-named function without changing behavior. Use when a block needs a comment to be understood, a function is long or mixes levels of abstraction, logic is duplicated, or a loop or conditional arm does several jobs. Scans the target file, proposes candidates and names, and edits only after approval.

inline-function

Replace a function call with the function's body when the indirection adds no meaning. Use when a function body is as clear as its name, a wrapper only delegates, a helper is used once and forces unnecessary navigation, or bad factoring should be undone by inlining before re-extracting. Scans the target file, proposes candidates, and edits only after approval.

refactoring-router

Entry-point refactoring and rewrite skill. Use when the user wants to refactor, clean up, improve, simplify, or rewrite a codebase/file/module and needs smell-driven analysis, technique selection, or a new implementation approach. Detects refactor vs rewrite intent, asks type/mode/risk/scope/depth or ambition, then dispatches specialized subagents in an automatic loop. Triggers: "refactor this", "rewrite this", "clean this up", "improve this codebase", "reimplement this".

関連スキル