This is a common issue that comes across in projects where there are frequent releases. After some period of time, the container registry will get filled up with images and consume too much space. Manual deletion is not a friendly option as we need to spend time frequently cleaning up all the unwanted images by going through the web GUI. The easiest option is to configure a policy.

GitLab provides a very easy way to perform this by configuring clean-up and retention policies. It also allows us options to protect images with specific tags to avoid deletion of critical or important images.

The steps are explained below.

  1. Go to your project in GitLab
  2. Navigate to:
    Settings –> Packages & Registries –> Container Registry
  3. Enable Cleanup policy
  4. Configure rules like:
    • Delete tags matching regex (e.g. .*)
    • Keep last N tags (e.g. keep 10)
    • Remove tags older than X days
    • Exclude tags like latest, prod, etc.

Example policy:

  • Remove tags older than 60 days
  • Keep 10 most recent tags
  • Protect: latest|main|prod

I hope this tip is useful. I have used this in several projects especially in the development phase as people keeps committing a lot of unwanted images and we end up consuming a lot of space and burning a lot of money.

Feel free to comment below if you have any questions or feedback.