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
print = codebase.log

feature_flag_usage = {}
feature_flag_class = codebase.get_class('FeatureFlag')

if feature_flag_class:
    # Initialize usage count for all attributes
    for attr in feature_flag_class.attributes:
        feature_flag_usage[attr.name] = 0

    # Get all usages of the FeatureFlag class
    for usage in feature_flag_class.usages():
        usage_source = usage.usage_symbol.source if hasattr(usage, 'usage_symbol') else str(usage)
        for flag_name in feature_flag_usage.keys():
            if f"FeatureFlag.{flag_name}" in usage_source:
                feature_flag_usage[flag_name] += 1

    sorted_flags = sorted(feature_flag_usage.items(), key=lambda x: x[1], reverse=True)

    print("Feature Flag Usage Table:")
    print("-------------------------")
    print(f"{'Feature Flag':<30} | {'Usage Count':<12}")
    print("-" * 45)
    for flag, count in sorted_flags:
        print(f"{flag:<30} | {count:<12}")

    print(f"\nTotal feature flags: {len(sorted_flags)}")
else:
    print("❗ FeatureFlag enum not found in the codebase")

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).

Was this page helpful?