Skip to main content
A cleanup agent that deletes preview environments once their pull request is merged or closed, or after 7 days without activity. It can delete whole environments of one project, except the long-lived ones you protect. Level: Hygiene. See all the agent policy examples.

What the agent does

  1. Lists the environments of the project and their last deployment.
  2. Checks the pull request behind each preview in your Git provider.
  3. Deletes the previews whose pull request is merged or closed, or that are idle for 7 days.
  4. Reports what it deleted.

What the policy allows

The agent can

  • Find the organization, project and environment by name
  • Read the environments of the project, except environment variables
  • Delete a whole environment of the project, unless it is protected

The agent cannot

  • Delete production, staging or any protected environment
  • Delete a single service, a database or the project
  • Delete anything in another project
  • Deploy, stop or change anything
Anything the policy does not allow is refused with 401 Unauthorized.

The policy

Copy it, then replace each REPLACE_WITH_... value with a real ID from your organization.
clean-up-preview-environments.rego
You can copy the IDs from the Console URL, or list them with qovery project list, qovery environment list and qovery application list.

Create the token

In a session signed in as an organization Owner or Admin, ask:
The qovery-policy-token skill finds the IDs, writes a policy like the one above, tests it locally and against the API, then creates the token.

Prompt for the agent

The agent runs with the Qovery Agent Skills and the Qovery CLI, in Claude Code, Codex, Cursor or any agent that supports skills. Give it the token through the environment, never in the prompt:
Then give the agent its task:
Every call the agent makes appears in the audit logs as policy:<token-id>:<token-name>.

Good to know

  • The policy cannot see whether an environment is a preview. It allows deleting any environment of the project except the ones in protected_environment_ids. Add every new long-lived environment to that list before you create the token.
  • Qovery can also delete a preview automatically when its pull request is closed. See preview environments. The agent is useful for the previews that stay open but idle.

All agent policy examples

Every use case, from read only to production changes.

API Policy Token (Preview)

How policies are evaluated, the input they see, and the limits.