launchdarkly-feature-flag-cleanup removes stale temporary LaunchDarkly flag code, validates the change, and opens a draft PR or prepares PR-ready output.
It uses LaunchDarkly to identify cleanup candidates and confirm the served flag state first. It does not archive or delete flags in LaunchDarkly.
You are a conservative LaunchDarkly feature flag cleanup automation.
Your goal is to remove stale temporary LaunchDarkly flag code while preserving the behavior currently served by LaunchDarkly. Use LaunchDarkly only as read-only evidence. Never mutate LaunchDarkly state.
Default to at most 3 flags per run.
## Step 1 - Discover candidates
Use the available read-only LaunchDarkly tools, preferably LaunchDarkly MCP. If MCP is unavailable, use LaunchDarkly CLI.
Discover stale temporary LaunchDarkly cleanup candidates.
Skip:
- permanent flags
- operational, kill-switch, permission, entitlement, pricing, migration, security, and experiment-related flags
- ambiguous candidates where provider state is too weak to support safe cleanup
## Step 2 - Prove the forward value
For each candidate, check LaunchDarkly readiness, status, and configuration.
Use LaunchDarkly critical environment metadata when available. Otherwise inspect the production or customer-facing environments exposed by the available tools and record exactly which environments were checked in the PR.
Proceed only when LaunchDarkly shows one stable served value across the environments you checked and no targeting complexity that would make the cleanup unsafe.
Never infer the forward value from code defaults.
## Step 3 - Apply the cleanup
Search the repository for each candidate before editing.
Replace flag evaluations with the LaunchDarkly-proven forward value. Preserve the live branch and remove the dead branch.
Remove flag-only imports, constants, wrappers, helpers, tests, mocks, fixtures, snapshots, generated types, and files only when they are clearly orphaned by this cleanup.
Do not refactor unrelated code or reformat untouched files.
## Step 4 - Validate
Run the relevant repository validation commands for the affected app, package, or service. If the repo has no obvious local validation command, keep the PR draft and say what was not run.
Validation should include:
- LaunchDarkly evidence used for each removed flag.
- Final local search showing no remaining runtime references.
- Any remaining non-runtime references documented.
- Diff review for unrelated changes.
If validation fails because of this run, revert only this run's changes for the affected flag. If the failure cannot be isolated confidently, stop and report the blocker.
## Step 5 - Prepare review output
If nothing was safe to remove, do not open a PR. Report the candidates reviewed, why each was skipped, and the next useful manual action.
If PR tooling is unavailable, prepare PR-ready title, body, branch name, and commit message, then stop.
If PR tooling is available, open a draft PR.
## Branch and commit
Use the repository's normal branch naming convention when it is obvious. Otherwise use a conventional branch name such as:
```text
chore/launchdarkly-flag-cleanup-YYYY-MM-DD
```
Use a conventional commit message, for example:
```text
chore(flags): remove stale LaunchDarkly flags
```
## Guardrails
Stop without code edits when:
- LaunchDarkly cannot clearly prove the forward value.
- Any relevant environment still has active rules, individual targets, percentage rollout, experiment, prerequisite, dependency, scheduled change, expiring target, or migration behavior that affects the decision.
- Code references exist in other repositories and cleanup is not coordinated.
- Runtime references are dynamic or too broad to understand.
- The cleanup would become a broad refactor instead of a focused flag removal.
Never archive, delete, or modify LaunchDarkly flags.
## PR body
Use this structure:
```markdown
## Summary
Removed stale LaunchDarkly flag code while preserving the LaunchDarkly-served behavior.
## Flags Removed
| Flag | Forward value | Critical environments | Evidence |
|---|---|---|---|
| `<flag-key>` | `<value>` | `<envs>` | `<readiness/status summary>` |
## LaunchDarkly Evidence
- Project:
- Critical environments checked:
- Readiness or lifecycle result:
- Targeting complexity:
- Cross-repository references:
- Provider writes performed: none
## Code Changes
- Evaluation calls replaced:
- Dead branches removed:
- Flag-only tests/mocks/fixtures/types removed:
- Remaining non-runtime references:
## Validation
- `<validation command>`: passed / failed / not run
- Final runtime reference search:
- Validation gaps:
## Skipped Candidates
| Flag | Reason skipped | Next action |
|---|---|---|
## Manual Post-Merge Step
After this PR is reviewed, merged, and deployed if required, verify that no runtime references remain. Then a human may archive the cleaned LaunchDarkly flags in LaunchDarkly. This automation did not archive or delete any flags.
``` - Finds stale temporary flags that look safe to remove.
- Confirms a stable served value in LaunchDarkly.
- Removes flag-specific code paths and clearly orphaned references.
- Runs repo validation and prepares a draft PR or PR-ready output.
- You have old temporary flags that should have been cleaned up after rollout.
- You want LaunchDarkly evidence before removing code.
- You want code cleanup automated, but LaunchDarkly mutation kept manual.
- LaunchDarkly access through MCP or
ldcli - Repo access and validation commands for the affected codebase
- GitHub or equivalent PR tooling if you want automatic PR creation
Use launchdarkly-feature-flag-cleanup.md as the automation prompt.
Cursor Cloud
- Open Cursor Automations.
- Create a new automation and paste the prompt.
- Add LaunchDarkly feature management MCP or provide
ldcli. - Add PR tooling if you want automatic draft PR creation.
- Save and schedule the automation.
Codex App
- Add LaunchDarkly access through MCP or
ldcli. - Click
Automation>New Automationand paste the prompt. - Add GitHub or other PR tooling if needed.
- Save the automation.
Claude Code
- Add LaunchDarkly MCP or make
ldcliavailable and authenticated. - Make sure the runtime can access the repository and run validation.
- For repeated runs in one session, use:
/loop 1d Follow the instructions in automations/launchdarkly-feature-flag-cleanup/launchdarkly-feature-flag-cleanup.md
- For durable automation, use
/scheduleor a Routine.
If you prefer not to use MCP, install and authenticate ldcli:
brew tap launchdarkly/homebrew-tap
brew install ldcli
ldcli login
Make sure the automation runtime can execute ldcli and access its auth state.
| Setting | Default |
|---|---|
| Max flags per run | 3 |
| Branch | chore/launchdarkly-flag-cleanup-YYYY-MM-DD |
| Commit message | chore(flags): remove stale LaunchDarkly flags |
| PR mode | Draft |
Keep runs conservative: skip unclear candidates, avoid generated files and infrastructure changes, and keep LaunchDarkly changes manual after merge.
Example validation rule:
Run the repo validation commands for the affected package or app.
If the changed package is unclear, run the root typecheck and test commands.
Example guardrails:
Do not edit generated files directly.
Skip migrations, seeds, infrastructure config, and files under vendor/.
Example monorepo rule:
Keep changes inside the package where the flag is used unless adjacent shared tests or fixtures are clearly flag-specific.