GuideFebruary 14, 20254 min read

What Is Dead Code? A Developer's Guide to Finding and Removing It

Dead code is unreachable or unused source code that adds complexity, increases bundle size, and slows down your team. Learn what causes it and how to remove it safely.

dead-codejavascripttypescriptcode-qualityrefactoring

Dead code is source code that exists in your project but is never executed, referenced, or reachable during runtime. It sits in your codebase doing nothing -- except making everything harder to maintain.

Every codebase accumulates dead code over time. Features get removed but their helper functions stay behind. Refactors replace old implementations without deleting them. Developers copy-paste code and forget to clean up the original. The result is a codebase that grows larger and more confusing with every sprint.

Types of Dead Code

Dead code comes in several forms, each with different detection strategies:

Unused Functions and Methods

Functions that are defined but never called from anywhere in the codebase. These are the most common form of dead code and often the easiest to detect with static analysis.

// This function was used by a feature that was removed last quarter
function calculateLegacyDiscount(price: number): number {
  return price * 0.85;
}

Unreachable Code

Code that exists after a return statement, inside an impossible condition, or behind a feature flag that will never be enabled.

function getStatus(isActive: boolean): string {
  return isActive ? "active" : "inactive";
  // Everything below this line is unreachable
  console.log("Processing status...");
  return "unknown";
}

Unused Imports

Import statements that bring in modules, functions, or types that are never used in the file. These are especially common in JavaScript and TypeScript projects.

import { formatDate, formatCurrency, formatPercentage } from "./utils";

// Only formatDate is actually used below
export function renderTimestamp(date: Date): string {
  return formatDate(date);
}

Orphaned Files

Entire files that are not imported by any other file in the project. These often result from moving functionality to a new location without deleting the old file.

Dead Feature Flags

Feature flags that have been fully rolled out (always true) or permanently disabled (always false) but remain in the code, adding unnecessary branching logic.

Why Dead Code Matters

Dead code is not just an aesthetic problem. It has real costs:

Larger bundle sizes. In frontend projects, dead code that is not tree-shaken ends up in your production bundle, increasing download times for users.

Slower developer onboarding. New team members read dead code thinking it matters, wasting hours understanding logic that is never executed.

Misleading search results. When you search for a function name or variable, dead code creates false matches that slow down debugging and code navigation.

Hidden security risks. Dead code may import deprecated libraries with known vulnerabilities. Since the code is never tested, these vulnerabilities go unnoticed.

Increased test burden. Teams sometimes write tests for dead code, wasting CI time and maintenance effort on code that does not run in production.

How to Detect Dead Code

Manual Code Review

The simplest approach is reading through your codebase and looking for unused exports, unreferenced functions, and orphaned files. This works for small projects but does not scale.

Static Analysis Tools

Language-specific tools can automatically detect dead code:

  • TypeScript/JavaScript: ts-prune finds unused exports, ESLint's no-unused-vars catches unused variables and imports
  • Swift: Periphery detects unused declarations across your entire Xcode project
  • Python: Vulture finds unused code by analyzing the AST

Automated Scanning

Tools like CleanAI combine multiple analysis tools into a single scan. You run one command and get a unified report of all dead code across your project, regardless of language.

How to Remove Dead Code Safely

Removing dead code requires care. Here is a safe workflow:

  1. Run a full scan to identify all dead code candidates
  2. Review each finding to confirm it is truly unused (some code may be called dynamically)
  3. Comment out first rather than deleting immediately
  4. Run your test suite to verify nothing breaks
  5. Build the project to catch any compilation errors
  6. Delete the commented code once you are confident it is safe

This comment-out-then-build approach is exactly what CleanAI's Auto Clean (Safe) feature automates. It comments out each finding one at a time, runs your build, and only confirms the removal if the build succeeds.

Preventing Dead Code Accumulation

The best strategy is preventing dead code from accumulating in the first place:

  • Delete code in the same PR that removes the feature -- do not leave cleanup for later
  • Enable ESLint rules like no-unused-vars and no-unused-imports in your CI pipeline
  • Run periodic scans (weekly or per-sprint) to catch dead code early
  • Track dead code metrics as part of your engineering health dashboard

Dead code is inevitable in any long-lived project, but with the right tools and habits, you can keep it under control.