Search code examples
google-cloud-platformgoogle-cloud-storagegcloudgoogle-artifact-registry

Command To Delete All Image Versions of Artifact Registry Except Latest?


I have a GitHub Action that pushes my image to Artifact Registry. This is the steps that authenticates and then pushes it to my Google Cloud Artifact Registry

      - name: Configure Docker Client
        run: |-
          gcloud auth configure-docker --quiet
          gcloud auth configure-docker $GOOGLE_ARTIFACT_HOST_URL --quiet
      - name: Push Docker Image to Artifact Registry
        run: |-
          docker tag $IMAGE_NAME:latest $GOOGLE_ARTIFACT_HOST_URL/$PROJECT_ID/images/$IMAGE_NAME:$GIT_TAG
          docker push $GOOGLE_ARTIFACT_HOST_URL/$PROJECT_ID/images/$IMAGE_NAME:$GIT_TAG


Where $GIT_TAG is always 'latest'

I want to add one more command that then purges all except the latest version. In this screenshot below, you see theres 2 images enter image description here

I would like to remove the one that was 3 days ago as its not the one with the tag 'latest'.

Is there a terminal command to do this?


Solution

  • You may initially check through the filtered list of container images for your specific criteria.

    gcloud artifacts docker images list --include-tags
    

    Once you have the view of the images to be deleted, move to the following.
    You may use the following command to delete an Artifact Registry container image.

    gcloud artifacts docker images delete IMAGE [--async] [--delete-tags] [GCLOUD_WIDE_FLAG …]

    A valid container image that can be referenced by tag or digest, has the format of

    LOCATION-docker.pkg.dev/PROJECT-ID/REPOSITORY-ID/IMAGE:tag

    This command can fail for the following reasons:

    • Trying to delete an image by digest when the image is still tagged. Add --delete-tags to delete the digest and the tags.
    • Trying to delete an image by tag when the image has other tags. Add --delete-tags to delete all tags.
    • A valid repository format was not provided.
    • The specified image does not exist.
    • The active account does not have permission to delete images.

    It is always recommended to check and reconfirm any deletion operations so you don’t lose any useful artifacts and data items. Also check this helpful document here for Artifacts docker Image Deletion guidelines and some useful information on Managing Images.

    As guillaume blaquiere mentioned you may have a look at this link which may help you.