Codegen’s GraphSitter library provides powerful APIs for managing feature flag usage in code. This guide will walk you through the basics of how to detect and clean up obsolete feature flags.

Removing rolled out flags

Once a feature has been fully rolled out behind a flag and is stable on production, the next step is to remove old fallback code paths.

Common syntax for feature flags include:

Deleting unused feature flags

Detecting unused feature flag is a specific use case of deleting dead code, isolated to a specific search space.

Delete unused flag
# Get the file containing feature flags
feature_flag_file = codebase.get_file("path/to/feature_flags.py")
# Enumerate through the feature flag definitions in file
feature_flags = feature_flag_file.global_vars

for flag_var in feature_flags:
    # Find flags not being referenced anywhere
    if not flag_var.get_usages():
        flag_var.remove()

Once rolled out feature flags are removed, new dead code paths may have been created. Run a delete dead code codemod to ensure complete clean-up.

Best Practices

  1. Use Caution: Ensure that flags have been fully rolled out before deleting.

  2. Fix Tests: Check for additional ways feature flags are used in the codebase (e.g. string arguments in test patches).