Delete a secret with ETags
Stay organized with collections
Save and categorize content based on your preferences.
Shows how to delete a secret with a given name, ETag, and all of its versions.
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"]],[],[],[],null,["# Delete a secret with ETags\n\nShows how to delete a secret with a given name, ETag, and all of its versions.\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Secret Manager, see\n[Secret Manager client libraries](/secret-manager/docs/reference/libraries).\n\n\nTo authenticate to Secret Manager, 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\n \tsecretmanager \"cloud.google.com/go/secretmanager/apiv1\"\n \t\"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb\"\n )\n\n // deleteSecretWithEtag deletes the secret with the given name and all of its versions.\n func deleteSecretWithEtag(name, etag string) error {\n \t// name := \"projects/my-project/secrets/my-secret\"\n \t// etag := `\"123\"`\n\n \t// Create the client.\n \tctx := context.Background()\n \tclient, err := secretmanager.https://cloud.google.com/go/docs/reference/cloud.google.com/go/secretmanager/latest/apiv1.html#cloud_google_com_go_secretmanager_apiv1_Client_NewClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create secretmanager client: %w\", err)\n \t}\n \tdefer client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/secretmanager/latest/apiv1.html#cloud_google_com_go_secretmanager_apiv1_Client_Close()\n\n \t// Build the request.\n \treq := &secretmanagerpb.DeleteSecretRequest{\n \t\tName: name,\n \t\tEtag: etag,\n \t}\n\n \t// Call the API.\n \tif err := client.DeleteSecret(ctx, req); err != nil {\n \t\treturn fmt.Errorf(\"failed to delete secret: %w\", err)\n \t}\n \treturn nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Secret Manager, see\n[Secret Manager client libraries](/secret-manager/docs/reference/libraries).\n\n\nTo authenticate to Secret Manager, 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 com.google.cloud.secretmanager.v1.https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.DeleteSecretRequest.html;\n import com.google.cloud.secretmanager.v1.https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.SecretManagerServiceClient.html;\n import com.google.cloud.secretmanager.v1.https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.SecretName.html;\n import java.io.IOException;\n\n public class DeleteSecretWithEtag {\n\n public static void deleteSecret() throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"your-project-id\";\n String secretId = \"your-secret-id\";\n // Including the quotes is important.\n String etag = \"\\\"1234\\\"\";\n deleteSecret(projectId, secretId, etag);\n }\n\n // Delete an existing secret with the given name and etag.\n public static void deleteSecret(String projectId, String secretId, String etag)\n throws IOException {\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 \"close\" method on the client to safely clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.SecretManagerServiceClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.SecretManagerServiceClient.html.create()) {\n // Build the secret name.\n https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.SecretName.html secretName = https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.SecretName.html.of(projectId, secretId);\n\n // Construct the request.\n https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.DeleteSecretRequest.html request =\n https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.DeleteSecretRequest.html.newBuilder()\n .setName(secretName.https://cloud.google.com/java/docs/reference/google-cloud-secretmanager/latest/com.google.cloud.secretmanager.v1.SecretName.html#com_google_cloud_secretmanager_v1_SecretName_toString__())\n .setEtag(etag)\n .build();\n\n // Delete the secret.\n client.deleteSecret(request);\n System.out.printf(\"Deleted secret %s\\n\", secretId);\n }\n }\n }\n\n### Python\n\n\nTo learn how to install and use the client library for Secret Manager, see\n[Secret Manager client libraries](/secret-manager/docs/reference/libraries).\n\n\nTo authenticate to Secret Manager, 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 def delete_secret_with_etag(project_id: str, secret_id: str, etag: str) -\u003e None:\n \"\"\"\n Delete the secret with the given name, etag, and all of its versions.\n \"\"\"\n\n # Import the Secret Manager client library and types.\n from google.cloud import secretmanager\n from google.cloud.secretmanager_v1.types import service\n\n # Create the Secret Manager client.\n client = secretmanager.https://cloud.google.com/python/docs/reference/secretmanager/latest/google.cloud.secretmanager_v1.services.secret_manager_service.SecretManagerServiceClient.html()\n\n # Build the resource name of the secret.\n name = client.https://cloud.google.com/python/docs/reference/secretmanager/latest/google.cloud.secretmanager_v1.services.secret_manager_service.SecretManagerServiceClient.html#google_cloud_secretmanager_v1_services_secret_manager_service_SecretManagerServiceClient_secret_path(project_id, secret_id)\n\n # Build the request\n request = service.https://cloud.google.com/python/docs/reference/secretmanager/latest/google.cloud.secretmanager_v1.types.DeleteSecretRequest.html()\n request.name = name\n request.etag = etag\n\n # Delete the secret.\n client.https://cloud.google.com/python/docs/reference/secretmanager/latest/google.cloud.secretmanager_v1.services.secret_manager_service.SecretManagerServiceClient.html#google_cloud_secretmanager_v1_services_secret_manager_service_SecretManagerServiceClient_delete_secret(request=request)\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=secretmanager)."]]