Noh | エンジニア向け情報共有コミュニティ
Signup / Login

If you are getting high bills from Artifact Registry on GCP every day, it might be because you have old container images still present.

gcp

投稿日: 2024/03/26

更新日: 2024/07/02

This article is a translation of the following article.

https://noh.ink/articles/34Nw4sBL8TJ0F4yDeBZZ

When I checked the GCP billing, I noticed a charge of about 20 yen per day under the service name Artifact Registry, and was paying a few hundred yen per month. Realizing it was strange, I confirmed that the container images created when deploying from Cloud Build to Cloud Run were accumulating significantly in Artifact Registry.

The images on this Artifact Registry will not be automatically deleted unless you set them yourself, so I will introduce the setting method from the management screen.

Target Audience of this Article

  • People who receive daily invoices under the service name Artifact Registry and find it burdensome
  • People who want to regularly delete images accumulated in Artifact Registry
  • People who have set up periodic deletion with a cleanup policy but the deletion is not working properly

In the past, the main method for adding and removing settings in cloudbuild.yaml was to use gcr-cleaner. However, at present, it seems that Artifact Registry itself has a cleanup policy feature, so we will use that instead. The cleanup policy in Artifact Registry seems to be in preview as of March 2024, but the readme of gcr-cleaner mentions the following, indicating that it might be better to use this in the future.

Caution

The functionality provided by this tool is now built directly into Artifact Registry! We are no longer accepting bug reports or feature requests.

Important point

First of all, please note that even if you make settings, they will not be deleted immediately. As written in the document for the policy application schedule, the settings will not take effect immediately, so please wait.

In the Cleanup policies section, select Delete artifacts.

Artifact Registry deletes and retains artifacts that match your policies using a background job that runs periodically. Changes should take effect within approximately one day.

Procedure

This time, I will only keep the two latest images and regularly delete older ones. Be careful not to accidentally delete necessary images as it may affect the service deployed on Cloud Run. I chose to keep it at two to reduce costs, but it's advisable to leave some room based on your usage.

Open the GCP management screen and proceed to Artifact Registry -> Repositories -> Repository name (in this case, cloud-run-source-deploy) -> Edit repository -> Image name. Then, press the edit repository button at the top.

Verify that Delete artifacts is selected in the Clean Up Policy section (I will skip the dry-run explanation.).

Click "Add Cleanup Policy" to add the following two policies:

The first setting is to keep the latest two images.

The second setting is to delete all images, except for the ones with specified retention settings.

An additional note is that the setting to retain is not enough to remove it.

When you check the image list after a while, you can confirm that it has been automatically deleted. For reference, in my case, I think it was deleted about an hour after setting it.

Reference

Official documentation for Clean-up Policy is available here

Here is how to manage the cleanup policy in JSON and apply it through commands.

Here is how to use gcr-cleaner to add configurations to cloudbuild.yaml and delete them.

Table of Contents