Automating Dead Code Removal with AI and Static Analysis
Dead Code Removal Sucks ☠️
Dead code is a perennial source of tech debt in large codebases. As unused code and outdated feature flags accumulate, the codebase gets weighed down with countless intricate sets of if-else statements and functions. Engineers tiptoe around them, scared to delete them in case of breaking something else, so the dead code paths just lurk around indefinitely.
Automated Solutions
Meta even wrote a whole internal tool, SCARF, that uses advanced code analysis to safely delete unused code. SCARF creates and traverses code dependency graphs across multiple languages, and across API calls, in order to determine whether a piece of code is truly safe to remove—and what else needs to be removed in conjunction with it. In five years, SCARF auto-deleted over 100M lines of code at Meta.
Unfortunately, SCARF is not open source. But you might be wondering: in the age of AI tools, is SCARF still necessary? Can we just make AI remove dead code for us?
In a smaller codebase, sure. But, in a large codebase with complex cross-functional dependencies, it’s hard to pass in enough context to an AI agent—or to make it conduct a thorough enough code analysis—to trust it to come up with consistently safe deletions.
That’s where a combined static analysis + AI tool can come in. Codegen combines code analysis with AI tools to auto-generate dead code deletion PRs that you can trust.

Let’s look at one very practical example of automated dead code deletion—feature flag deletion.
Feature Flag Deletion
Feature flags are a particularly pernicious form of dead code buildup. As Anton Zaides writes:
- Each feature flag increases your testing burden by 2x – you need to test the feature with and without the flag.
- Feature flags take up tons of time even to maintain, let alone delete. Anton recounts seeing a developer spend a whole day refactoring code under a feature flag that was never even released.
Feature flags can inflate your costs, too, especially if you’re using a feature management service like LaunchDarkly that charges for each feature flag or connection.
With Codegen, you can connect your Github repo, type in the name of a feature flag you want to remove, and get an auto-generated diff removing the flag.

Here’s a diff that Codegen produced when we entered the flag name VALIDATE_CARD_ELIGIBILITY_BEFORE_SA_CREATION:

Because Codegen has indexed the entire codebase into a dependency graph, we can guarantee that each feature flag removal is comprehensive and non-breaking.
Though we chose a straightforward example for this blog post, you can imagine much more complex cases where a feature flag spans many files across the codebase.
Want to try this on your own codebase? Request a demo here.
