Delete a regional disk
Stay organized with collections
Save and categorize content based on your preferences.
This sample deletes a regional disk from a project.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis document provides code samples in Go, Java, and Python for deleting a regional disk from a Google Cloud project.\u003c/p\u003e\n"],["\u003cp\u003eThe samples demonstrate how to authenticate to Compute Engine using Application Default Credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe process of deleting a regional disk involves specifying the project ID, region, and disk name.\u003c/p\u003e\n"],["\u003cp\u003eEach sample uses a client library for the respective language to send a deletion request and wait for the operation to complete.\u003c/p\u003e\n"],["\u003cp\u003eIt includes instructions to setup each language before use, and also a link to find more code samples for other google cloud products.\u003c/p\u003e\n"]]],[],null,["# Delete a regional disk\n\nThis sample deletes a regional disk from a project.\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tcompute \"cloud.google.com/go/compute/apiv1\"\n \tcomputepb \"cloud.google.com/go/compute/apiv1/computepb\"\n )\n\n // deleteRegionalDisk permanently deletes a regional disk.\n func deleteRegionalDisk(w io.Writer, projectID, region, diskName string) error {\n \t// projectID := \"your_project_id\"\n \t// region := \"us-west3\"\n \t// diskName := \"your_disk_name\"\n\n \tctx := context.Background()\n \tdisksClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_RegionDisksClient_NewRegionDisksRESTClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewDisksRESTClient: %w\", err)\n \t}\n \tdefer disksClient.Close()\n\n \treq := &computepb.DeleteRegionDiskRequest{\n \t\tProject: projectID,\n \t\tRegion: region,\n \t\tDisk: diskName,\n \t}\n\n \top, err := disksClient.Delete(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"unable to delete disk: %w\", err)\n \t}\n\n \tif err = op.Wait(ctx); err != nil {\n \t\treturn fmt.Errorf(\"unable to wait for the operation: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Disk deleted\\n\")\n\n \treturn nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Java API\nreference documentation](/java/docs/reference/google-cloud-compute/latest/overview).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Operation.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.RegionDisksClient.html;\n import java.io.IOException;\n import java.util.concurrent.ExecutionException;\n import java.util.concurrent.TimeUnit;\n import java.util.concurrent.TimeoutException;\n\n public class RegionalDelete {\n\n public static void main(String[] args)\n throws IOException, ExecutionException, InterruptedException, TimeoutException {\n // TODO(developer): Replace these variables before running the sample.\n // Project ID or project number of the Cloud project you want to use.\n String project = \"YOUR_PROJECT_ID\";\n\n // Name of the region where the disk is located.\n String region = \"europe-central2\";\n\n // Name of the disk you want to delete.\n String diskName = \"YOUR_DISK_NAME\";\n\n deleteRegionalDisk(project, region, diskName);\n }\n\n // Deletes a disk from a project.\n public static void deleteRegionalDisk(String project, String region, String diskName)\n throws IOException, ExecutionException, InterruptedException, TimeoutException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests. After completing all of your requests, call\n // the `regionDisksClient.close()` method on the client to safely\n // clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.RegionDisksClient.html regionDisksClient = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.RegionDisksClient.html.create()) {\n\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Operation.html operation = regionDisksClient.deleteAsync(project, region, diskName)\n .get(3, TimeUnit.MINUTES);\n\n if (operation.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Operation.html#com_google_cloud_compute_v1_Operation_hasError__()) {\n System.out.println(\"Disk deletion failed!\");\n throw new https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Error.html(operation.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Operation.html#com_google_cloud_compute_v1_Operation_getError__().toString());\n }\n System.out.println(\"Operation Status: \" + operation.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Operation.html#com_google_cloud_compute_v1_Operation_getStatus__());\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from __future__ import annotations\n\n import sys\n from typing import Any\n\n from google.api_core.extended_operation import ExtendedOperation\n from google.cloud import https://cloud.google.com/python/docs/reference/compute/latest/\n\n\n def wait_for_extended_operation(\n operation: ExtendedOperation, verbose_name: str = \"operation\", timeout: int = 300\n ) -\u003e Any:\n \"\"\"\n Waits for the extended (long-running) operation to complete.\n\n If the operation is successful, it will return its result.\n If the operation ends with an error, an exception will be raised.\n If there were any warnings during the execution of the operation\n they will be printed to sys.stderr.\n\n Args:\n operation: a long-running operation you want to wait on.\n verbose_name: (optional) a more verbose name of the operation,\n used only during error and warning reporting.\n timeout: how long (in seconds) to wait for operation to finish.\n If None, wait indefinitely.\n\n Returns:\n Whatever the operation.result() returns.\n\n Raises:\n This method will raise the exception received from `operation.exception()`\n or RuntimeError if there is no exception set, but there is an `error_code`\n set for the `operation`.\n\n In case of an operation taking longer than `timeout` seconds to complete,\n a `concurrent.futures.TimeoutError` will be raised.\n \"\"\"\n result = operation.result(timeout=timeout)\n\n if operation.error_code:\n print(\n f\"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}\",\n file=sys.stderr,\n flush=True,\n )\n print(f\"Operation ID: {operation.name}\", file=sys.stderr, flush=True)\n raise operation.exception() or RuntimeError(operation.error_message)\n\n if operation.warnings:\n print(f\"Warnings during {verbose_name}:\\n\", file=sys.stderr, flush=True)\n for warning in operation.warnings:\n print(f\" - {warning.code}: {warning.message}\", file=sys.stderr, flush=True)\n\n return result\n\n\n def delete_regional_disk(project_id: str, region: str, disk_name: str) -\u003e None:\n \"\"\"\n Deletes a disk from a project.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n region:name of the region where the disk is located.\n disk_name: name of the disk that you want to delete.\n \"\"\"\n disk_client = https://cloud.google.com/python/docs/reference/compute/latest/.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.region_disks.RegionDisksClient.html()\n operation = disk_client.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.region_disks.RegionDisksClient.html#google_cloud_compute_v1_services_region_disks_RegionDisksClient_delete(project=project_id, region=region, disk=disk_name)\n wait_for_extended_operation(operation, \"regional disk deletion\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]