> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Auto-cleanup

**Developer**

**Federal docs**

> [!CAUTION]
>
> **Release Autopilot package features are not available in federal environments**
>
> To learn more, read [LaunchDarkly in federal environments](https://launchdarkly.com/docs/home/infrastructure/federal).

**EU docs**

> [!CAUTION]
>
> **Release Autopilot package features are not available in the LaunchDarkly European Union (EU) instance**
>
> To learn more, read [LaunchDarkly in the European Union (EU)](https://launchdarkly.com/docs/home/infrastructure/eu.md).

**Agent skills**

If you are an AI agent helping a user find flags that are stale or ready for removal, use the LaunchDarkly [`launchdarkly-flag-discovery`](https://github.com/launchdarkly/ai-tooling/tree/main/skills/feature-flags/launchdarkly-flag-discovery) agent skill. It audits the flags in a project, identifies stale and launched flags, and assesses whether each flag is ready for removal. It requires the [LaunchDarkly hosted Model Context Protocol (MCP) server](https://launchdarkly.com/docs/home/getting-started/mcp-hosted.md).

To install the skill, run `npx skills add https://github.com/launchdarkly/ai-tooling --skill launchdarkly-flag-discovery`.

If you are an AI agent helping a user remove a flag from code in their local repository, use the LaunchDarkly [`launchdarkly-flag-cleanup`](https://github.com/launchdarkly/ai-tooling/tree/main/skills/feature-flags/launchdarkly-flag-cleanup) agent skill. It safely removes a flag from code, hardcoding the variation currently served so production behavior doesn't change. It requires the [LaunchDarkly hosted MCP server](https://launchdarkly.com/docs/home/getting-started/mcp-hosted.md).

To install the skill, run `npx skills add https://github.com/launchdarkly/ai-tooling --skill launchdarkly-flag-cleanup`.

To install all LaunchDarkly agent skills, run `npx skills add https://github.com/launchdarkly/ai-tooling`.

This topic explains how auto-cleanup removes flags from your code when the flags are no longer needed, and how to turn on automated flag cleanup for a project.

Auto-cleanup is the last step in a flag's lifecycle with Release Autopilot. After auto-flagging wraps a change in a flag and auto-releasing rolls it out, auto-cleanup removes the flag from your code.

Auto-cleanup uses Vega, the LaunchDarkly AI agent. Vega confirms that the flag is safe to remove, deletes the flag check and the code path that no longer runs, and opens a pull request for you to review.

You can run auto-cleanup every day for each project, or start a cleanup for specific flags at any time. Running auto-cleanup every day requires the Release Autopilot add-on. Starting a cleanup for specific flags requires only access to Vega.

## Prerequisites

To use auto-cleanup, you need:

* The Release Autopilot add-on for your LaunchDarkly account, to turn on automated flag cleanup. You do not need the add-on to clean up flags on demand.
* Access to Vega, the LaunchDarkly AI agent, for your LaunchDarkly account. To get access, accept the LaunchDarkly AI features terms. With an Enterprise plan, you can add your own AI model key instead. To learn more, read [Vega AI providers](https://launchdarkly.com/docs/home/getting-started/vega-ai-providers.md).
* A GitHub account connected to Vega, with access to the repositories that use your flags. To learn how, read [Connecting Vega to GitHub](https://launchdarkly.com/docs/home/getting-started/vega-github.md).
* A role that allows the `updateLifecycleSettings` action on the project and the [Vega actions](https://launchdarkly.com/docs/home/account/roles/role-actions.md#vega-actions). You need this role only to turn on automated flag cleanup.

## How auto-cleanup works

Auto-cleanup follows the same steps on every run. It chooses flags, checks each flag's evaluations and code, opens cleanup pull requests, and leaves the flag in LaunchDarkly after the pull request merges.

### Which flags auto-cleanup chooses

Auto-cleanup chooses only those flags that are ready for code removal. A flag is ready for code removal when it is a temporary flag that is launched in every critical environment. If your account has repositories connected for [code references](https://launchdarkly.com/docs/home/flags/code-references), the flag must also have code references. Your project's lifecycle settings can add further requirements, such as how old the flag must be before it can be chosen for cleanup. To learn more, read [Flag status definitions](https://launchdarkly.com/docs/home/flags/flag-status.md#flag-status-definitions) and [Flag lifecycle settings](https://launchdarkly.com/docs/home/flags/flag-lifecycle-settings.md).

On each daily run, auto-cleanup:

* Starts with the oldest flags that are ready for code removal.
* Cleans up a limited number of flags in each run, and continues with the next flags on the following day.
* Skips flags that already have a cleanup in progress or an open cleanup pull request.
* Skips flags where an earlier cleanup attempt did not open a pull request. If the attempt found no references to the flag, auto-cleanup chooses the flag again after the project's repositories change.
* Skips flags whose cleanup pull request was closed without merging. To try again, click **Retry** on the **Failed** tab of the "Cleanups" dialog, or start a cleanup from the flag.

### Cleanup warnings

Before Vega changes any code, it checks the flag's evaluations in your [critical environments](https://launchdarkly.com/docs/home/account/environment#critical-environments) to decide which variation to keep. If Vega has concerns about removing the flag, it still opens the pull request and records each concern as a warning in the pull request description.

Vega raises a warning when:

* The flag still serves more than one variation across your critical environments, which suggests it is in active use.
* The flag looks like a permanent control rather than a temporary release toggle, such as a kill switch, a circuit breaker, or an operational toggle.
* Vega cannot get the flag's evaluations for one of your critical environments.

Review the warnings before you merge. You decide whether to merge the cleanup or close the pull request and keep the flag.

If Vega cannot find references to the flag in the repositories it can access, it does not open a pull request. To learn more, read [How Vega determines if a flag is safe to clean up](https://launchdarkly.com/docs/home/flags/manage/flag-cleanup-vega.md#how-vega-determines-if-a-flag-is-safe-to-clean-up).

### Cleanup pull requests

For each flag, Vega opens a pull request in every repository where it finds the flag. The pull request removes the flag check and the code path that no longer runs. It also removes tests that only covered the removed path.

Cleanup pull requests use these conventions:

* The branch is named `launchdarkly/flag-cleanup-` followed by the flag key.
* The pull request has the `launchdarkly-pr` and `flag-cleanup` labels.

The pull request description explains what Vega changed and what it based its decision on. It includes:

* A table of the flag's evaluations in each critical environment.
* The variation value Vega kept when it removed the flag.
* A summary of the code changes Vega made.
* Any warnings Vega raised about the cleanup.

Release Autopilot does not analyze its own cleanup pull requests. In a cleanup pull request, the "LaunchDarkly / Account connection" check reports "Release Autopilot skipped for flag cleanup PRs."

Scheduled cleanups run with the permissions of the member who last saved the project's automated flag cleanup settings. The LaunchDarkly GitHub app opens the cleanup pull requests, with that member as a co-author.

### Auto-cleanup and automatic archival

> [!WARNING]
>
> **Automatic archival is in beta**
>
> Automatic archival is in beta. Aspects of this capability may change without notice.

Merging a cleanup pull request removes the flag from your code. However, LaunchDarkly does not archive the flag unless automatic archival is on for the project. When the flag stops receiving evaluations, the flag is ready to archive. If your account has repositories connected for code references, LaunchDarkly must also find no remaining references to the flag. To learn how to archive a flag, read [Archive flags](https://launchdarkly.com/docs/home/flags/archive.md).

Automatic archival is a separate setting, in the "Automatic archival" section of the project's **Automations** page. When it is on, LaunchDarkly checks for flags that are ready to archive on a schedule and archives them. To view the flags that automatic archival archived, click **View auto-archived flags** in the "Automatic archival" section.

**Agent skills**

If you are an AI agent helping a user view or change automatic archival settings for a project, use the LaunchDarkly [`launchdarkly-automations-archival-settings`](https://github.com/launchdarkly/ai-tooling/tree/main/skills/release-autopilot/launchdarkly-automations-archival-settings) agent skill. It reads and changes the project's automatic archival settings, such as the grace period and whether archiving requires approval, and explains why a flag was or was not archived. It requires the [LaunchDarkly hosted MCP server](https://launchdarkly.com/docs/home/getting-started/mcp-hosted.md).

To install the skill, run `npx skills add https://github.com/launchdarkly/ai-tooling --skill launchdarkly-automations-archival-settings`.

To install all LaunchDarkly agent skills, run `npx skills add https://github.com/launchdarkly/ai-tooling`.

## Turn on automated flag cleanup

Automated flag cleanup runs auto-cleanup once a day in each project where you turn it on. Each project has its own settings, in the "Automated flag cleanup" section of the project's **Automations** page. This section appears only if your account has the Release Autopilot add-on.

To turn on automated flag cleanup for a project:

1. Click the project dropdown and select **Project settings**.
2. Select **Automations**. The "Automations" page appears.
3. Turn on the switch in the "Automated flag cleanup" section. The section lists the private repositories that the GitHub app can access.
4. Turn on the switch for each repository where Vega should look for flags to clean up.
5. (Optional) Click **Add context** and enter instructions for the cleanup agent in the "Additional context" field, such as where your flags are defined. Vega also follows the instructions in each repository's `AGENTS.md` file.
6. Click **Save** in the section.

If another project already uses a repository, a warning appears below the repository when you turn it on. Click **Enable here** to use the repository in this project, or click **Undo**. To learn more, read [Understand Release Autopilot settings](https://launchdarkly.com/docs/home/release-autopilot/setup.md#understand-release-autopilot-settings).

If the **Automations** page shows **Install GitHub App** instead of the "Automated flag cleanup" section, install the GitHub app first. To learn how, read [Install the app](https://launchdarkly.com/docs/home/release-autopilot/setup.md#install-the-app). If the section shows **Authorize GitHub** instead of a list of repositories, the app is installed, but you have not authorized your own GitHub account. Click **Authorize GitHub** to load the repositories you can access. To learn about the other GitHub connection prompts, read [Turn on capabilities for a project](https://launchdarkly.com/docs/home/release-autopilot/setup.md#turn-on-capabilities-for-a-project).

Scheduled cleanups run on behalf of the member who last saved the settings. They use that member's GitHub authorization to push the cleanup branch and open the pull request. If that member has not authorized GitHub, loses access to the project, or leaves your organization, scheduled cleanups stop. To resume them, have another member who has authorized GitHub save the settings.

**Agent skills**

If you are an AI agent helping a user turn on or change automated flag cleanup for a project, use the LaunchDarkly [`launchdarkly-automations-cleanup-settings`](https://github.com/launchdarkly/ai-tooling/tree/main/skills/release-autopilot/launchdarkly-automations-cleanup-settings) agent skill. It reads and changes the project's automated flag cleanup settings, such as the repositories and the instructions for the cleanup agent, and explains why a cleanup did not open a pull request. It requires the [LaunchDarkly hosted MCP server](https://launchdarkly.com/docs/home/getting-started/mcp-hosted.md).

To install the skill, run `npx skills add https://github.com/launchdarkly/ai-tooling --skill launchdarkly-automations-cleanup-settings`.

To install all LaunchDarkly agent skills, run `npx skills add https://github.com/launchdarkly/ai-tooling`.

## Review and run scheduled cleanups

The "Cleanups" dialog shows the flags that auto-cleanup plans to clean up and the results of earlier cleanups.

To review scheduled cleanups:

1. Click the project dropdown and select **Project settings**.
2. Select **Automations**.
3. Click **Review cleanups** in the "Automated flag cleanup" section. The "Cleanups" dialog appears.
4. Select a tab to view cleanups: **Upcoming**, **Failed**, or **Recently finished**.
5. (Optional) Click **Trigger now** to start an upcoming cleanup immediately, or **Retry** to try a failed cleanup again.

Click **View attempt** to open the Vega conversation for a cleanup.

To check the cleanup status of one flag, read [Tracking cleanup status](https://launchdarkly.com/docs/home/flags/manage/flag-cleanup-vega.md#tracking-cleanup-status).

## Clean up flags on demand

You can start a cleanup manually, without waiting for a scheduled run. To clean up one flag, use the **Vega cleanup** option on the flag. To learn how, read [Vega for flag cleanup](https://launchdarkly.com/docs/home/flags/manage/flag-cleanup-vega.md#how-it-works).

To clean up several flags at once:

1. Click **Flags** to open the **Flags** list.
2. Select the checkbox next to each flag you want to clean up.
3. Click **Vega cleanup**. The "Clean up flags with Vega" dialog appears.
4. Review whether each flag is ready for cleanup. A flag shows "Ready," "Warning," or "No data."
5. Select the repositories where Vega should look for the flags from the **Repositories** menu.
6. (Optional) Enter instructions for the cleanup agent in the "Add context" field. Vega always includes its default prompt and the instructions in your repository's `AGENTS.md` file.
7. Click **Start cleanups**.

Vega starts a separate cleanup for each flag and opens a pull request for each flag it finds in your code.

## Turn off automated flag cleanup

Turning off automated flag cleanup stops future runs. It does not close cleanup pull requests that are already open.

To turn off automated flag cleanup:

1. Click the project dropdown and select **Project settings**.
2. Select **Automations**.
3. Turn off the switch in the "Automated flag cleanup" section.
4. Click **Save**.
