google-app-enginegoogle-cloud-platformgcloudgoogle-container-registry

How can I list and delete GCP AppEngine images from the command line?


I'd like to delete my old unused AppEngine images from Google Cloud Platform, so that I'm not charged for storing them.

I can manually list and delete the images created for my GCP AppEngine project from this URL: https://console.cloud.google.com/gcr/images/GOOGLE_CLOUD_PROJECT_ID

(Obviously, replace GOOGLE_CLOUD_PROJECT_ID with an appropriate GCP project id.)

Is there a way to list them from the command line? e.g. via gcloud?

This doesn't work as I would expect:

$ gcloud compute images list --no-standard-images
Listed 0 items.

Neither does this:

$ gcloud container images list 
Listed 0 items.
Only listing images in gcr.io/GOOGLE_CLOUD_PROJECT_ID. Use --repository to list images in other repositories.

It's a little painful to go in and delete lots of these manually since each image under https://console.cloud.google.com/gcr/images/GOOGLE_CLOUD_PROJECT_ID/US/appengine is in a separate directory that I have to first click into to select the image and then click the delete button, and then go back out to the appengine directory and start the process again for any other images.


Solution

  • Listing images

    Thanks to LundinCast's answer, I used us.gcr.io as the hostname and I was able to do this:

    $ gcloud container images list --repository=us.gcr.io/GCP_PROJECT_ID
    NAME
    us.gcr.io/GCP_PROJECT_ID/appengine
    

    Apparently AppEngine uses a nested repository:

    $ gcloud container images list --repository=us.gcr.io/GCP_PROJECT_ID/appengine
    NAME
    us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
    us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726
    us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t162317
    us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181113t132223
    us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181113t150454
    us.gcr.io/GCP_PROJECT_ID/appengine/default.20181108t184629
    us.gcr.io/GCP_PROJECT_ID/appengine/default.20181108t205831
    us.gcr.io/GCP_PROJECT_ID/appengine/DEF.20181113t163644
    

    This didn't work as I expected though:

    $ gcloud container images list --repository=us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
    Listed 0 items.
    

    Instead, I had to do:

    $ gcloud container images list-tags us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425 
    DIGEST        TAGS    TIMESTAMP
    c8e3797d36b2  latest  2018-11-10T12:57:58
    

    Deleting images

    According to the documentation for deleting images, it is possible to delete ...

    Deleting images by (implicit) tag

    When I tried to delete the single version / tag, I was shown a prompt:

    $ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425 
    WARNING: Implicit ":latest" tag specified: us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
    Digests:
    - us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425@sha256:c8e3797d36b27599c91e231afd5521e6a1283319fce5a0fd978b7cdcdb1d4181
      Associated tags:
     - latest
    Tags:
    - us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425:latest
    This operation will delete the tags and images identified by the 
    digests above.
    
    Do you want to continue (Y/n)?  n
    
    ERROR: (gcloud.container.images.delete) Aborted by user.
    

    This is a little problematic for writing a script or program that can delete old images automatically (although I could pipe the yes command into the gcloud container images delete IMAGE command); luckily there is a --quiet argument that can be used:

    $ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425 --quiet
    WARNING: Implicit ":latest" tag specified: us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
    Digests:
    - us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425@sha256:c8e3797d36b27599c91e231afd5521e6a1283319fce5a0fd978b7cdcdb1d4181
      Associated tags:
     - latest
    Tags:
    - us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425:latest
    Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425:latest].
    Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425@sha256:c8e3797d36b27599c91e231afd5521e6a1283319fce5a0fd978b7cdcdb1d4181].
    

    So, that worked.

    Deleting images by digest

    Trying again on another image, with the digest method of deleting images:

    $ gcloud container images list-tags us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726
    DIGEST        TAGS    TIMESTAMP
    4d860e73f85f  latest  2018-11-10T15:50:55
    

    Surprise! You can't just use the digest as listed:

    $ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@4d860e73f85f
    ERROR: (gcloud.container.images.delete) [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@4d860e73f85f] digest must be of the form "sha256:<digest>".
    

    This was a little odd (there's only one tag, "latest", and it's not even shown in the error) :

    $ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85f
    ERROR: Tags:
    ERROR: (gcloud.container.images.delete) This operation will implicitly delete the tags listed above. Please manually remove with the `untag` command or re-run with --force-delete-tags to confirm.
    

    OK, let's use --force-delete-tags :

    $ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85f --force-delete-tags
    Digests:
    - us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85fdddb485e85dc867653e3e6095a5225f4f670200c98ede10fb542
      Associated tags:
     - latest
    This operation will delete the tags and images identified by the digests above.
    
    Do you want to continue (Y/n)?  n
    
    ERROR: (gcloud.container.images.delete) Aborted by user.
    

    As before, let's use --quiet to prevent showing the prompt:

    $ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85f --force-delete-tags --quiet
    Digests:
    - us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85fdddb485e85dc867653e3e6095a5225f4f670200c98ede10fb542
      Associated tags:
     - latest
    Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726:latest].
    Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85fdddb485e85dc867653e3e6095a5225f4f670200c98ede10fb542].
    

    So, deleting by digest requires a more verbose command line.