Remove stale LaunchDarkly flags automatically with GitHub Actions
Flags that reach 100% in production tend to stay in the code for months. LaunchDarkly knows which ones are done. flagrm can ask it, remove those flags from the code, check the result against a baseline and open a pull request for each one. Here is how to set that up, from the config to a weekly GitHub Action.
1. What “stale” means
A flag is ready to remove from code when it has stopped being a switch. flagrm stale reads your LaunchDarkly project and calls a flag remove only when every one of these holds:
- It is a boolean flag, and every checked environment serves
trueto every context. No rules, individual targets, prerequisites or percentage rollouts. Targeting that is off withtrueas the off variation counts too. - It has served
truefor at leastminDaysOndays. The count starts at the latest targeting change in the audit log, so renaming the flag or editing its description doesn’t reset it. - It is marked temporary in LaunchDarkly, and carries none of the tags you exclude (
permanentby default). - Exactly one code name in this repository matches it.
Everything else is keep, with the reasons listed, or unreferenced when the repository has no matching code. An unreferenced flag isn’t a suggestion to archive it, because another repository may still read it.
2. Configure the LaunchDarkly section
Add a launchdarkly: block to flagrm.config.yaml. To remove flags that have been ON for four weeks and aren’t permanent:
# flagrm.config.yaml
launchdarkly:
project: shop # your LaunchDarkly project key
environments: [production] # only production has to serve true
minDaysOn: 28 # four weeks
temporaryOnly: true # only flags marked temporary qualify
tags: { exclude: [permanent] }
map: { checkout-v2: NewCheckout } # only when the key and the code name differ
temporaryOnly: true and excluding permanent are already the defaults. They’re written out here so the rule is visible in the file. minDaysOn defaults to 30.
Without environments, every environment of the project must serve true, which is rarely what you want once developers have their own. map is optional: new-checkout already matches NewCheckout when you ignore case and -, _, . and spaces.
The API token never goes in the file. flagrm reads it from the variable named by tokenEnv (LD_API_TOKEN by default) and sends it only to baseUrl. A token with the Reader role is enough: flagrm only makes GET requests and never changes anything in LaunchDarkly.
3. Try it locally first
export LD_API_TOKEN=api-xxxxxxxx
npx flagrm doctor # warns if the token variable isn't set
npx flagrm stale --md # a readable summary; --json has the full result
Read the verdicts before automating anything. If a flag you expected is keep, the reasons say why: still targeted somewhere, on for too few days, permanent, an ambiguous code name. Exit code 0 means flagrm could evaluate, whatever the verdicts. Exit 2 is a config problem, and exit 3 means LaunchDarkly was unreachable or refused the request. That includes a wrong project key, since LaunchDarkly answers the same way when the token belongs to another organization.
4. Remove one flag
Run the removal for a single flag in your agent first, for example /flagrm-remove NewCheckout in Claude Code. When the config has a launchdarkly: section, the skill runs flagrm stale NewCheckout --json instead of asking you whether the flag is ON everywhere, so nobody has to be there to answer. It then follows the usual procedure from the first guide: baseline, edits, flagrm verify, with the Stop hook keeping the agent working until verify passes.
5. Run it weekly with GitHub Actions
On Azure DevOps instead? See the Azure Pipelines version.
flagrm ships a ready workflow in examples/ci/github-actions.yml. Copy it to .github/workflows/flagrm-stale.yml on the default branch, because GitHub runs scheduled workflows only from there. Before the first run, check that the repository has:
- flagrm as a dev dependency, so
npm ciinstalls it, and the Claude Code setup fromflagrm initcommitted: theflagrm-removeandflagrm-reviewskills and the Stop hook. - Two repository secrets (Settings, Secrets and variables, Actions):
LD_API_TOKENandANTHROPIC_API_KEY. - Permission for workflows to open pull requests. Under Settings, Actions, General, Workflow permissions, select Allow GitHub Actions to create and approve pull requests. It is off by default in personal repositories, and an organization can turn it off for all of its repositories. Without it,
gh pr createfails after the branch is pushed.
The workflow runs in two jobs.
The first job picks the flags
on:
schedule:
- cron: "0 6 * * 1" # Mondays 06:00 UTC
workflow_dispatch:
permissions:
contents: read
jobs:
stale:
runs-on: ubuntu-latest
outputs:
flags: ${{ steps.pick.outputs.flags }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 22 }
- run: npm ci
- name: Ask LaunchDarkly which flags are ready
env:
LD_API_TOKEN: ${{ secrets.LD_API_TOKEN }}
run: npx --no-install flagrm stale --json > "$RUNNER_TEMP/stale.json"
# ...then keep the "remove" verdicts that have no open removal branch
If LaunchDarkly can’t be reached or the config is wrong, the step fails and no agent starts. The picking step drops any flag whose branch flagrm/remove-<flag> already exists, so a flag with an open pull request isn’t removed twice, and it skips names that aren’t safe to use in a branch name.
The second job removes each flag and opens a pull request
remove:
needs: stale
if: needs.stale.outputs.flags != '[]'
runs-on: ubuntu-latest
permissions:
contents: write # push the removal branch
pull-requests: write # open the pull request
strategy:
fail-fast: false
max-parallel: 2
matrix:
flag: ${{ fromJSON(needs.stale.outputs.flags) }}
env:
FLAG: ${{ matrix.flag }} # through the environment, never interpolated into a script
steps:
# checkout, setup-node, your build toolchain, npm ci, install Claude Code
- name: Remove the flag with Claude Code
run: claude -p "/flagrm-remove $FLAG" --allowedTools "Bash,Read,Edit,Write,Glob,Grep"
- name: Verify the removal
run: npx --no-install flagrm verify "$FLAG" --json > "$RUNNER_TEMP/verify.json"
- name: Review the removal in a fresh session
continue-on-error: true
run: claude -p "/flagrm-review $FLAG" --permission-mode dontAsk ...
- name: Open a pull request
run: |
git switch -c "flagrm/remove-$FLAG"
git add -A && git commit -m "chore: remove $FLAG feature flag"
git push origin "flagrm/remove-$FLAG"
gh pr create --title "chore: remove $FLAG feature flag" --body-file ...
Each stale flag gets its own job and its own pull request. The workflow token can only read the repository, except in this job, which may push a branch and open a pull request. Three safeguards apply when nobody is watching the run:
- The pipeline runs
flagrm verifyitself. The agent saying it’s done is not enough. If verify fails, the job stops before a branch is pushed. - A second agent reviews the diff.
/flagrm-reviewruns in a new process without the removal’s context. It can’t edit anything: no Edit or Write tool, and Bash only forflagrm verifyandgit diff. Its findings go into the pull request in a collapsed block. The review is advisory, and if it fails the pull request still opens and says so. - Nothing merges by itself. A person reviews every pull request.
The agent job runs your build and tests, so add your toolchain to the job, such as actions/setup-dotnet. Pull requests opened with the default GITHUB_TOKEN don’t start other workflows, so if your CI checks must run on them, use a GitHub App or a fine-grained token for GH_TOKEN.
6. Review, merge, then archive
Each pull request body holds the flagrm verify report: leftovers, new dead code, build, tests and lint against the baseline. Review it like any other change, and check the places the checks can’t see (see the blind spots).
Once the change is deployed, archive the flag in LaunchDarkly. Doing it earlier makes deployed code that still evaluates the flag fall back to its default, which may be the OFF path.
7. What to keep in mind
- One repository at a time. flagrm sees this repository’s code. A flag that another service also reads can come back as
removehere, so keeptemporaryOnly: trueand mark shared flags permanent or tag them. - The first run can open several pull requests. A backlog of old flags all qualifies at once.
max-parallel: 2limits the load. Start the first run by hand (Actions, flagrm stale flags, Run workflow) and look at what it opens before you leave it on the schedule. - Schedules can pause. In a public repository, GitHub disables scheduled workflows after 60 days without repository activity. Scheduled runs can also start late when GitHub Actions is under heavy load.
- It costs agent tokens. One removal run plus one review per flag. The review reuses verify’s build and test runs, so it costs no rebuild.
- Removals are only as good as your tests. If a flag’s code has no test coverage,
verifyhas less to compare against.