launchdarkly-flag-cleanup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

LaunchDarkly Flag Cleanup

You're using a skill that will guide you through safely removing a feature flag from a codebase while preserving production behavior. Your job is to explore the codebase to understand how the flag is used, query LaunchDarkly to determine the correct forward value, remove the flag code cleanly, and verify the result.
If you haven't already identified which flag to clean up, use the flag discovery skill first to audit the landscape and find candidates.
你正在使用一项技能,它将引导你安全地从代码库中移除功能标志,同时保留生产环境的行为。你的任务是探索代码库以了解标志的使用方式,查询LaunchDarkly以确定正确的目标值,干净地移除标志代码,并验证结果。
如果你还未确定要清理的标志,请先使用标志发现技能来审核现状并找到候选标志。

Prerequisites

前提条件

This skill requires the remotely hosted LaunchDarkly MCP server to be configured in your environment.
Required MCP tools:
  • check-removal-readiness
    — detailed safety check (orchestrates flag config, cross-env status, dependencies, code references, and expiring targets in parallel)
  • get-flag
    — fetch flag configuration for a specific environment
Optional MCP tools:
  • archive-flag
    — archive the flag in LaunchDarkly after code removal
  • delete-flag
    — permanently delete the flag (irreversible, prefer archive)
此技能要求在你的环境中配置远程托管的LaunchDarkly MCP服务器。
必需的MCP工具:
  • check-removal-readiness
    — 详细的安全检查(并行协调标志配置、跨环境状态、依赖项、代码引用和过期目标)
  • get-flag
    — 获取特定环境的标志配置
可选的MCP工具:
  • archive-flag
    — 在代码移除后在LaunchDarkly中归档标志
  • delete-flag
    — 永久删除标志(不可逆,优先选择归档)

Core Principles

核心原则

  1. Safety First: Always preserve current production behavior.
  2. LaunchDarkly as Source of Truth: Never guess the forward value. Query the actual configuration.
  3. Follow Conventions: Respect existing code style and structure.
  4. Minimal Change: Only remove flag-related code. No unrelated refactors.
  1. 安全第一:始终保留当前生产环境行为。
  2. 以LaunchDarkly为事实来源:绝不猜测目标值,查询实际配置。
  3. 遵循约定:尊重现有代码风格和结构。
  4. 最小变更:仅移除与标志相关的代码,不进行无关的重构。

Workflow

工作流程

Step 1: Explore the Codebase

步骤1:探索代码库

Before touching LaunchDarkly or removing code, understand how this flag is used in the codebase.
  1. Find all references to the flag key. Search for the flag key string (e.g.,
    new-checkout-flow
    ) across the codebase. Check for:
    • Direct SDK evaluation calls (
      variation()
      ,
      boolVariation()
      ,
      useFlags()
      , etc.)
    • Constants/enums that reference the key
    • Wrapper/service patterns that abstract the SDK
    • Configuration files, tests, and documentation
    • See SDK Patterns for the full list of patterns by language
  2. Understand the branching. For each reference, identify:
    • What code runs when the flag is
      true
      (or variation A)?
    • What code runs when the flag is
      false
      (or variation B)?
    • Are there side effects, early returns, or nested conditions?
  3. Note the scope. How many files, components, or modules does this flag touch? A flag used in one
    if
    block is simpler than one threaded through multiple layers.
在接触LaunchDarkly或移除代码之前,先了解此标志在代码库中的使用方式。
  1. 查找标志键的所有引用:在整个代码库中搜索标志键字符串(例如
    new-checkout-flow
    )。检查:
    • 直接SDK评估调用(
      variation()
      boolVariation()
      useFlags()
      等)
    • 引用该键的常量/枚举
    • 抽象SDK的包装器/服务模式
    • 配置文件、测试和文档
    • 查看SDK模式获取按语言分类的完整模式列表
  2. 理解分支逻辑:对于每个引用,确定:
    • 标志为
      true
      (或变体A)时运行哪些代码?
    • 标志为
      false
      (或变体B)时运行哪些代码?
    • 是否有副作用、提前返回或嵌套条件?
  3. 注意范围:此标志涉及多少文件、组件或模块?仅在一个
    if
    块中使用的标志比贯穿多个层级的标志更简单。

Step 2: Run the Removal Readiness Check

步骤2:运行移除就绪性检查

Use
check-removal-readiness
to get a detailed safety assessment. This single tool call orchestrates multiple checks in parallel:
  • Flag configuration and targeting state
  • Cross-environment status
  • Dependent flags (prerequisites)
  • Expiring targets
  • Code reference statistics
The tool returns a readiness verdict:
safe
— No blockers or warnings. Proceed with removal.
caution
— No hard blockers but warnings exist (e.g., code references in other repos, expiring targets scheduled, flag marked as permanent). Present warnings and let the user decide.
blocked
— Hard blockers prevent safe removal (e.g., dependent flags, actively receiving requests, targeting is on with active rules). Present blockers — the user must resolve them first.
使用
check-removal-readiness
获取详细的安全评估。这个单一工具调用会并行协调多项检查:
  • 标志配置和目标状态
  • 跨环境状态
  • 依赖标志(前置条件)
  • 过期目标
  • 代码引用统计
该工具会返回就绪性结论:
safe
— 无阻碍或警告,可继续移除。
caution
— 无硬性阻碍但存在警告(例如其他仓库中的代码引用、已计划的过期目标、标志被标记为永久)。呈现警告并让用户决定。
blocked
— 存在硬性阻碍阻止安全移除(例如依赖标志、正在接收请求、目标规则处于激活状态)。呈现阻碍,用户必须先解决这些问题。

Step 3: Determine the Forward Value

步骤3:确定目标值

Use
get-flag
to fetch the flag configuration in each critical environment. The forward value is the variation that replaces the flag in code.
ScenarioForward Value
All critical envs ON, same fallthrough, no rules/targetsUse
fallthrough.variation
All critical envs OFF, same offVariationUse
offVariation
Critical envs differ in ON/OFF stateNOT SAFE — stop and inform the user
Critical envs serve different variationsNOT SAFE — stop and inform the user
使用
get-flag
获取每个关键环境的标志配置。目标值是将在代码中替换标志的变体。
场景目标值
所有关键环境开启,回退值相同,无规则/目标使用
fallthrough.variation
所有关键环境关闭,offVariation相同使用
offVariation
关键环境的开启/关闭状态不同不安全 — 停止并通知用户
关键环境提供不同的变体不安全 — 停止并通知用户

Step 4: Remove the Flag from Code

步骤4:从代码中移除标志

Now execute the removal using what you learned in Step 1.
  1. Replace flag evaluations with the forward value.
    • Preserve the code branch matching the forward value
    • Remove the dead branch entirely
    • If the flag value was assigned to a variable, replace the variable with the literal value or inline it
  2. Clean up dead code.
    • Remove imports, constants, and type definitions that only existed for the flag
    • Remove functions, components, or files that only existed for the dead branch
    • Check for orphaned exports, hooks, helpers, styles, and test files
    • If the repo uses an unused-export tool (Knip, ts-prune, lint rules), run it and remove any flag-related orphans
  3. Don't over-clean.
    • Only remove code directly related to the flag
    • Don't refactor, optimize, or "improve" surrounding code
    • Don't change formatting or style of untouched code
Example transformation (boolean flag, forward value =
true
):
typescript
// Before
const showNewCheckout = await ldClient.variation('new-checkout-flow', user, false);
if (showNewCheckout) {
  return renderNewCheckout();
} else {
  return renderOldCheckout();
}

// After
return renderNewCheckout();
现在使用步骤1中学到的知识执行移除操作。
  1. 用目标值替换标志评估
    • 保留与目标值匹配的代码分支
    • 完全移除废弃分支
    • 如果标志值被分配给变量,将变量替换为字面值或内联它
  2. 清理废弃代码
    • 移除仅为该标志存在的导入、常量和类型定义
    • 移除仅为废弃分支存在的函数、组件或文件
    • 检查孤立的导出、钩子、辅助函数、样式和测试文件
    • 如果仓库使用未使用导出工具(Knip、ts-prune、lint规则),运行它并移除任何与标志相关的孤立项
  3. 不过度清理
    • 仅移除与标志直接相关的代码
    • 不要重构、优化或“改进”周围的代码
    • 不要更改未触及代码的格式或样式
示例转换(布尔标志,目标值 =
true
):
typescript
// Before
const showNewCheckout = await ldClient.variation('new-checkout-flow', user, false);
if (showNewCheckout) {
  return renderNewCheckout();
} else {
  return renderOldCheckout();
}

// After
return renderNewCheckout();

Step 5: Create Pull Request

步骤5:创建Pull Request

Use the template in references/pr-template.md for a structured PR description. The PR should clearly communicate:
  • What flag was removed and why
  • What the forward value is and why it's correct
  • The readiness assessment results (from
    check-removal-readiness
    )
  • What code was removed and what behavior is preserved
  • Whether other repos still reference this flag
使用references/pr-template.md中的模板来编写结构化的PR描述。PR应清晰说明:
  • 移除了哪个标志以及原因
  • 目标值是什么以及为什么它是正确的
  • 就绪性评估结果(来自
    check-removal-readiness
  • 移除了哪些代码以及保留了哪些行为
  • 是否有其他仓库仍引用此标志

Step 6: Verify

步骤6:验证

Before considering the job done:
  1. Code compiles and lints. Run the project's build and lint steps.
  2. Tests pass. If the flag was used in tests, the tests should be updated to reflect the hardcoded behavior.
  3. No remaining references. Search the codebase one more time for the flag key to make sure nothing was missed.
  4. PR is complete. The description covers the readiness assessment, forward value rationale, and any cross-repo coordination needed.
在完成任务之前:
  1. 代码编译并通过lint检查:运行项目的构建和lint步骤。
  2. 测试通过:如果标志在测试中被使用,应更新测试以反映硬编码的行为。
  3. 无剩余引用:再次搜索代码库中的标志键,确保没有遗漏。
  4. PR完成:描述涵盖了就绪性评估、目标值的理由以及任何跨仓库协调需求。

Edge Cases

边缘情况

SituationAction
Flag not found in LaunchDarklyInform user, check for typos in the key
Flag already archivedAsk if code cleanup is still needed (flag is gone from LD but code may still reference it)
Multiple SDK patterns in codebaseSearch all patterns:
variation()
,
boolVariation()
,
variationDetail()
,
allFlags()
,
useFlags()
, plus any wrappers
Dynamic flag keys (
flag-${id}
)
Warn that automated removal may be incomplete — manual review required
Different default values in code vs LDFlag as inconsistency in the PR description
Orphaned exports/files remain after removalRun unused-export checks and remove dead files
情况操作
在LaunchDarkly中未找到标志通知用户,检查键是否有拼写错误
标志已归档询问是否仍需要代码清理(标志已从LD中移除,但代码可能仍引用它)
代码库中存在多种SDK模式搜索所有模式:
variation()
boolVariation()
variationDetail()
allFlags()
useFlags()
,以及任何包装器
动态标志键(
flag-${id}
警告自动化移除可能不完整 — 需要人工审核
代码与LD中的默认值不同在PR描述中标记为不一致
移除后仍存在孤立的导出/文件运行未使用导出检查并移除废弃文件

What NOT to Do

禁止操作

  • Don't change code unrelated to flag cleanup.
  • Don't refactor or optimize beyond flag removal.
  • Don't remove flags still being actively rolled out.
  • Don't guess the forward value — always query LaunchDarkly.
  • 不要更改与标志清理无关的代码。
  • 不要进行超出标志移除的重构或优化。
  • 不要移除仍在积极发布中的标志。
  • 不要猜测目标值 — 始终查询LaunchDarkly。

After Cleanup

清理后

Once the PR is merged and deployed:
  1. Archive the flag in LaunchDarkly using
    archive-flag
    . Archival is reversible; deletion is not. Always archive first.
  2. Notify other teams if
    check-removal-readiness
    reported code references in other repositories.
  3. If the flag had targeting changes pending, they can be ignored — the flag is being removed.
一旦PR合并并部署:
  1. 使用
    archive-flag
    在LaunchDarkly中归档标志
    。归档是可逆的;删除则不可逆。始终优先选择归档。
  2. 通知其他团队如果
    check-removal-readiness
    报告其他仓库中存在代码引用。
  3. 如果标志有未决的目标变更,可以忽略它们 — 标志正在被移除。

References

参考资料

  • PR Template — Structured PR description for flag removal
  • SDK Patterns — Flag evaluation patterns by language/framework
  • Flag Discovery — Find cleanup candidates before using this skill
  • Flag Targeting — If you need to change targeting instead of removing
  • PR模板 — 标志移除的结构化PR描述
  • SDK模式 — 按语言/框架分类的标志评估模式
  • 标志发现 — 在使用此技能之前找到清理候选标志
  • 标志目标设置 — 如果你需要更改目标设置而不是移除标志