Delete a custom image

You can only delete custom images that you, or someone who has access to the project, have added.

Before you begin

  • Read the Images document.
  • If you haven't already, set up authentication. Authentication is the process by which your identity is verified for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine as follows.

    Select the tab for how you plan to use the samples on this page:


    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.


    1. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. Set a default region and zone.


    To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

    1. Install the Google Cloud CLI.
    2. To initialize the gcloud CLI, run the following command:

      gcloud init
    3. Create local authentication credentials for your Google Account:

      gcloud auth application-default login

    For more information, see Set up authentication for a local development environment.


    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.

      Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

Delete a custom image

Use one of the following methods to delete the image.


  1. In the Google Cloud console, go to the Images page.

    Go to Images

  2. Check the box to the left of the image you want to delete.

  3. Click Delete at the top of the page. Your image is deleted.


Use the gcloud compute images delete command to delete an image:

gcloud compute images delete IMAGE_NAME

Replace IMAGE_NAME with the name of the image to delete.


from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from import compute_v1

def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

        Whatever the operation.result() returns.

        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    result = operation.result(timeout=timeout)

    if operation.error_code:
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
        print(f"Operation ID: {}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result

def delete_image(project_id: str, image_name: str) -> None:
    Deletes a disk image.

        project_id: project ID or project number of the Cloud project you use.
        image_name: name of the image you want to delete.
    image_client = compute_v1.ImagesClient()
    operation = image_client.delete(project=project_id, image=image_name)
    wait_for_extended_operation(operation, "image deletion")


Make a POST request to the images().delete method. Specify the name of the image you want to delete.


Replace the following:

  • PROJECT_ID: the project to which the image belongs.
  • RESOURCE_ID: the name of the image that you want to delete.