Delete a Kafka topic
Stay organized with collections
Save and categorize content based on your preferences.
Delete a Kafka topic
Explore further
For detailed documentation that includes this code sample, see the following:
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 Kafka topic\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Delete a Google Cloud Managed Service for Apache Kafka topic](/managed-service-for-apache-kafka/docs/delete-topic)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Managed Service for Apache Kafka quickstart using\nclient libraries](/managed-service-for-apache-kafka/docs/reference/libraries#install).\n\n\nFor more information, see the\n[Managed Service for Apache Kafka Go API\nreference documentation](https://cloud.google.com/go/docs/reference/cloud.google.com/go/managedkafka/latest/apiv1).\n\n\nTo authenticate to Managed Service for Apache Kafka, 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 \t\"cloud.google.com/go/managedkafka/apiv1/managedkafkapb\"\n \t\"google.golang.org/api/option\"\n\n \tmanagedkafka \"cloud.google.com/go/managedkafka/apiv1\"\n )\n\n func deleteTopic(w io.Writer, projectID, region, clusterID, topicID string, opts ...option.ClientOption) error {\n \t// projectID := \"my-project-id\"\n \t// region := \"us-central1\"\n \t// clusterID := \"my-cluster\"\n \t// topicID := \"my-topic\"\n \tctx := context.Background()\n \tclient, err := managedkafka.https://cloud.google.com/go/docs/reference/cloud.google.com/go/managedkafka/latest/apiv1.html#cloud_google_com_go_managedkafka_apiv1_Client_NewClient(ctx, opts...)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"managedkafka.NewClient got err: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tclusterPath := fmt.Sprintf(\"projects/%s/locations/%s/clusters/%s\", projectID, region, clusterID)\n \ttopicPath := fmt.Sprintf(\"%s/topics/%s\", clusterPath, topicID)\n \treq := &managedkafkapb.DeleteTopicRequest{\n \t\tName: topicPath,\n \t}\n \tif err := client.DeleteTopic(ctx, req); err != nil {\n \t\treturn fmt.Errorf(\"client.DeleteTopic got err: %w\", err)\n \t}\n \tfmt.Fprint(w, \"Deleted topic\\n\")\n \treturn nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Managed Service for Apache Kafka quickstart using\nclient libraries](/managed-service-for-apache-kafka/docs/reference/libraries#install).\n\n\nFor more information, see the\n[Managed Service for Apache Kafka Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/overview).\n\n\nTo authenticate to Managed Service for Apache Kafka, 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.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html;\n import com.google.cloud.managedkafka.v1.https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.ManagedKafkaClient.html;\n import com.google.cloud.managedkafka.v1.https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.TopicName.html;\n import java.io.IOException;\n\n public class DeleteTopic {\n\n public static void main(String[] args) throws Exception {\n // TODO(developer): Replace these variables before running the example.\n String projectId = \"my-project-id\";\n String region = \"my-region\"; // e.g. us-east1\n String clusterId = \"my-cluster\";\n String topicId = \"my-topic\";\n deleteTopic(projectId, region, clusterId, topicId);\n }\n\n public static void deleteTopic(String projectId, String region, String clusterId, String topicId)\n throws Exception {\n try (https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.ManagedKafkaClient.html managedKafkaClient = https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.ManagedKafkaClient.html.create()) {\n // This operation is being handled synchronously.\n managedKafkaClient.deleteTopic(https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.TopicName.html.of(projectId, region, clusterId, topicId));\n System.out.println(\"Deleted topic\");\n } catch (IOException | https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html e) {\n System.err.printf(\"managedKafkaClient.deleteTopic got err: %s\", e.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ErrorDetails.html#com_google_api_gax_rpc_ErrorDetails__T_getMessage_java_lang_Class_T__());\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Managed Service for Apache Kafka quickstart using\nclient libraries](/managed-service-for-apache-kafka/docs/reference/libraries#install).\n\n\nFor more information, see the\n[Managed Service for Apache Kafka Python API\nreference documentation](https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest).\n\n\nTo authenticate to Managed Service for Apache Kafka, 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 google.api_core.exceptions import NotFound\n from google.cloud import https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/\n\n # TODO(developer)\n # project_id = \"my-project-id\"\n # region = \"us-central1\"\n # cluster_id = \"my-cluster\"\n # topic_id = \"my-topic\"\n\n client = https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.services.managed_kafka.ManagedKafkaClient.html()\n\n topic_path = client.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.services.managed_kafka.ManagedKafkaClient.html#google_cloud_managedkafka_v1_services_managed_kafka_ManagedKafkaClient_topic_path(project_id, region, cluster_id, topic_id)\n request = https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.types.DeleteTopicRequest.html(name=topic_path)\n\n try:\n client.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.services.managed_kafka.ManagedKafkaClient.html#google_cloud_managedkafka_v1_services_managed_kafka_ManagedKafkaClient_delete_topic(request=request)\n print(\"Deleted topic\")\n except NotFound as e:\n print(f\"Failed to delete topic {topic_id} with error: {e.message}\")\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=managedkafka)."]]