I have routed all gcr.io traffic to Artifact Registry. The command gcloud container images delete --force-delete-tags
used to delete digests in an image along with the image. Now with the images hosted on Artifact Registry, the command still deletes digests but leaves an empty image. I would like to be able to delete the image via command line for automation purposes. How can I do this?
You can, in fact, perform commands on gcr.io container images routed to artifact registry using gcloud artifacts
commands. The trick is to use the correct location in the LOCATION-docker.pkg.dev
syntax, which proved difficult to find. For gcr.io images the location is just us
, giving us-docker.pkg.dev
.
Therefore, for an image that looks like
us.gcr.io/my-project/appengine/default.12345
use this command to delete it:
gcloud artifacts docker images delete
us-docker.pkg.dev/my-project/us.gcr.io/appengine/default.12345
It took trying a lot of things, but I finally got it by clicking on “Setup Instructions” in the console in the repo, which showed the valuable “us-docker.pkg.dev” in instructions for configuring Docker.
My suggestion for the console is that at the top left where you can see and copy the repository path, for gcr.io repos, don’t have us.gcr.io/my-project
like is currently there, but have us-docker.pkg.dev/my-project/us.gcr.io
instead.
Since you’re now using Artifact Registry, you should use gcloud artifacts docker images delete. To delete an image with all of its digests and tags, use the following command:
gcloud artifacts docker images delete us-west1-docker.pkg.dev/my-project/my-repository/busy-box
Make sure that you follow the valid container image format when you’re deleting an image:
LOCATION-docker.pkg.dev/PROJECT-ID/REPOSITORY-ID/IMAGE
Edit: The reason you’re only deleting the digest in the image is because gcloud container images delete
requires you to use the format *.gcr.io/PROJECT_ID/IMAGE_PATH@sha256:DIGEST
or *.gcr.io/PROJECT_ID/IMAGE_PATH:TAG
. These are Container Registry paths which could explain the reason that your image stays in the Artifact Registry repository.
As a workaround, Take a look at Clean up images in Container Registry. Commands such as gcloud container images delete --force-delete-tags
only deletes the image in the corresponding Artifact Registry gcr.io repository. It doesn’t actually delete the image stored in Container Registry. To remove Container Registry images, you have to delete the Cloud storage buckets for each Container Registry hostname.
To delete the bucket, use the gsutil rm command with the -r flag:
gsutil rm -r gs://BUCKET-NAME
Change the bucket name with the Container Registry storage bucket name along with your Project-ID: us.artifacts.PROJECT-ID.appspot.com
Feel free to edit this post.
1