Search code examples
gitlabgitlab-cicicd

Automatically delete stale environments in Gitlab


I am looking for ways to delete stale environments from GitLab.

Is there any automated approach to clean up stale environments on a schedule without manually deleting each environment? I am hoping to run the "stop environment" option, which is wired to perform a deletion of resources used in the environment as part of this clean-up process.


Solution

  • Depending on how you define a stale environment, a neat way to automatically clean up environments used for feature deployments is to use the auto_stop_in key.

    With this setting the environment will be automatically stopped after the defined timespan which is newly set after each deployment.

    review_app:
      script: deploy-review-app
      environment:
        name: review/$CI_COMMIT_REF_SLUG
        on_stop: stop_review_app
        auto_stop_in: 1 week
      rules:
        - if: $CI_MERGE_REQUEST_ID