home / skills / bizshuk / llm_plugin / clean-unused-code

clean-unused-code skill

/skills/clean-unused-code

This skill helps you identify and remove unused code in Go projects using deadcode, streamlining maintenance and reducing binary size.

npx playbooks add skill bizshuk/llm_plugin --skill clean-unused-code

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.6 KB
---
name: clean-unused-code
description: Find and remove unused code in projects. This skill should be used when cleaning up dead code, removing unreachable functions, or optimizing codebase by eliminating unused code. Currently supports Golang projects.
---

# Clean Unused Code

## Overview

This skill provides workflows for finding and removing unused code in projects. It uses static analysis tools to identify unreachable functions and dead code.

## When to Use

Use this skill when:

- Cleaning up dead code in a project
- Removing unreachable functions
- Optimizing codebase size
- Preparing for code review or release

## Supported Languages

| Language | Tool       | Install Command                                     |
| -------- | ---------- | --------------------------------------------------- |
| `golang` | `deadcode` | `go install golang.org/x/tools/cmd/deadcode@latest` |

---

## Golang Projects

### Prerequisites

Install the `deadcode` tool:

```bash
go install golang.org/x/tools/cmd/deadcode@latest
```

### Step 1: Find Unused Code

Run `deadcode` from the project root:

```bash
deadcode ./...
```

For projects with test dependencies, include tests:

```bash
deadcode -test ./...
```

### Step 2: Parse Output

The output format is:

```
<file>:<line>:<column>: unreachable func: <function_name>
```

Example output:

```
internal/utils/helper.go:45:1: unreachable func: unusedHelper
pkg/service/old.go:12:1: unreachable func: deprecatedMethod
```

### Step 3: Review and Remove

For each unreachable function:

1. **Verify** it is safe to remove (not called via reflection, not an interface implementation)
2. **Remove** the function definition
3. **Remove** any related imports that become unused
4. **Run** `go build ./...` to verify no build errors

### Step 4: Clean Up Imports

After removing functions, clean up unused imports:

```bash
goimports -w .
```

Or use `gopls` organize imports functionality.

---

## Caveats

> [!WARNING]
> `deadcode` may not detect functions called via:
>
> - Reflection (`reflect.ValueOf().MethodByName()`)
> - Interface implementations used through dependency injection
> - CGo bindings
> - Plugin systems

### When to Skip Removal

Do NOT remove functions if:

- They implement an interface (even if not directly called)
- They are exported and part of a public API/library
- They are called via reflection
- They are test helpers used in `_test.go` files (use `-test` flag)

---

## Verification

After cleaning, verify the project:

```bash
# Build check
go build ./...

# Test check
go test ./...

# Run deadcode again to confirm
deadcode ./...
```

Overview

This skill finds and helps remove unused code in projects, focusing on Golang today. It streamlines discovery of unreachable functions and dead code so you can safely shrink and clarify the codebase. Use it to lower maintenance cost and reduce accidental complexity before releases or refactors.

How this skill works

The skill runs static analysis to list functions and symbols that appear unreachable from the build graph. It parses tool output to present file, line, and function candidates, then guides manual verification steps and cleanup commands. It also recommends follow-up checks like rebuilding, testing, and organizing imports.

When to use it

  • Before a major refactor to remove cruft and simplify code paths
  • When preparing a repository for release or open-source publication
  • To reduce binary or package size by eliminating dead code
  • During code review to justify removals of unused functions
  • When cleaning technical debt accumulated over time

Best practices

  • Install and run the static analyzer from the project root and include tests when needed
  • Manually verify each candidate—watch for reflection, interface implementations, plugins, or CGO usage
  • Prefer conservative removals: delete functions only after build and test pass
  • Remove any newly unused imports and run an import-organizing tool
  • Re-run the analyzer after changes to confirm no remaining dead code

Example use cases

  • Run analysis on a monorepo to identify legacy helper functions no longer referenced
  • Clean a library before publishing to ensure exported API is intentional
  • Trim unused code paths introduced during feature prototyping
  • Prepare a minimal reproducible example by removing unrelated dead functions
  • Validate that dependency-injected implementations are still required before removal

FAQ

Will the tool find functions called via reflection?

No. Static analysis typically cannot detect reflective calls; verify such functions manually before removal.

Can I automatically delete everything the analyzer reports?

No. Treat results as candidates. Confirm they are not part of public API, interface implementations, or used by external consumers.

What follow-up checks should I run after removals?

Run a full build, all tests, and re-run the analyzer. Also run an import organizer to remove orphaned imports.