Delete a Google Cloud Managed Service for Apache Kafka topic
Stay organized with collectionsSave and categorize content based on your preferences.
To delete a single topic, you can use the Google Cloud console, the Google Cloud CLI,
the client library, the Managed Kafka API, or the open source
Apache Kafka APIs.
Required roles and permissions to delete a topic
To get the permissions that
you need to delete a topic,
ask your administrator to grant you the
Managed Kafka Topic Editor(roles/managedkafka.topicEditor)
IAM role on your project.
For more information about granting roles, seeManage access to projects, folders, and organizations.
This predefined role contains
the permissions required to delete a topic. To see the exact permissions that are
required, expand theRequired permissionssection:
Required permissions
The following permissions are required to delete a topic:
Deleting a topic isirreversibleand results in thepermanent lossof
all data stored in that topic. Ensure that you have appropriate backups or have
exported any necessary data before proceeding.
Ensure that you stop or reconfigure any consumers subscribed to the topic
to consume from a different topic before deleting the topic.
To delete a topic, follow these steps:
Console
In the Google Cloud console console, go to theClusterspage.
At the bottom of the Google Cloud console, aCloud Shellsession starts and displays a command-line prompt. Cloud Shell is a shell environment
with the Google Cloud CLI
already installed and with values already set for
your current project. It can take a few seconds for the session to initialize.
This command removes the specified topic from the designated
Google Cloud Managed Service for Apache Kafka cluster. All data associated with the topic is deleted,
and the topic is longer accessible to producers or consumers.
Replace the following:
TOPIC_ID: The ID of the topic to delete.
CLUSTER_ID: The ID of the cluster where the topic is located.
importcom.google.api.gax.rpc.ApiException;importcom.google.cloud.managedkafka.v1.ManagedKafkaClient;importcom.google.cloud.managedkafka.v1.TopicName;importjava.io.IOException;publicclassDeleteTopic{publicstaticvoidmain(String[]args)throwsException{// TODO(developer): Replace these variables before running the example.StringprojectId="my-project-id";Stringregion="my-region";// e.g. us-east1StringclusterId="my-cluster";StringtopicId="my-topic";deleteTopic(projectId,region,clusterId,topicId);}publicstaticvoiddeleteTopic(StringprojectId,Stringregion,StringclusterId,StringtopicId)throwsException{try(ManagedKafkaClientmanagedKafkaClient=ManagedKafkaClient.create()){// This operation is being handled synchronously.managedKafkaClient.deleteTopic(TopicName.of(projectId,region,clusterId,topicId));System.out.println("Deleted topic");}catch(IOException|ApiExceptione){System.err.printf("managedKafkaClient.deleteTopic got err: %s",e.getMessage());}}}
Python
fromgoogle.api_core.exceptionsimportNotFoundfromgoogle.cloudimportmanagedkafka_v1# TODO(developer)# project_id = "my-project-id"# region = "us-central1"# cluster_id = "my-cluster"# topic_id = "my-topic"client=managedkafka_v1.ManagedKafkaClient()topic_path=client.topic_path(project_id,region,cluster_id,topic_id)request=managedkafka_v1.DeleteTopicRequest(name=topic_path)try:client.delete_topic(request=request)print("Deleted topic")exceptNotFoundase:print(f"Failed to delete topic{topic_id}with error:{e.message}")
[[["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"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Delete a Google Cloud Managed Service for Apache Kafka topic\n\nTo delete a single topic, you can use the Google Cloud console, the Google Cloud CLI,\nthe client library, the Managed Kafka API, or the open source\nApache Kafka APIs.\n\nRequired roles and permissions to delete a topic\n------------------------------------------------\n\n\nTo get the permissions that\nyou need to delete a topic,\n\nask your administrator to grant you the\n\n\nManaged Kafka Topic Editor(`roles/managedkafka.topicEditor`)\nIAM role on your project.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nThis predefined role contains\n\nthe permissions required to delete a topic. To see the exact permissions that are\nrequired, expand the **Required permissions** section:\n\n\n#### Required permissions\n\nThe following permissions are required to delete a topic:\n\n- Delete a topic: ` ``managedkafka.topics.delete`\n\n\nYou might also be able to get\nthese permissions\nwith [custom roles](/iam/docs/creating-custom-roles) or\nother [predefined roles](/iam/docs/roles-overview#predefined).\n\nFor more information about this role, see\n[Managed Service for Apache Kafka predefined roles](/managed-service-for-apache-kafka/docs/access-control#predefined-roles).\n\nDelete a topic\n--------------\n\nDeleting a topic is *irreversible* and results in the *permanent loss* of\nall data stored in that topic. Ensure that you have appropriate backups or have\nexported any necessary data before proceeding.\n\nEnsure that you stop or reconfigure any consumers subscribed to the topic\nto consume from a different topic before deleting the topic.\n\nTo delete a topic, follow these steps: \n\n### Console\n\n1. In the Google Cloud console console, go to the **Clusters** page.\n\n [Go to Clusters](https://console.cloud.google.com/managedkafka/clusters)\n2. From the list of clusters, click the cluster to which the topic that you want to delete belongs. The **Cluster details** page opens. In the cluster details page,\n for the **Resources** tab, the topics are listed.\n\n3. Click the topic that you want to delete. The **Topic details** page opens.\n\n4. Click **Delete** and confirm the operation.\n\n### gcloud\n\n1. In the Google Cloud console, activate Cloud Shell.\n\n [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n\n\n At the bottom of the Google Cloud console, a\n [Cloud Shell](/shell/docs/how-cloud-shell-works)\n session starts and displays a command-line prompt. Cloud Shell is a shell environment\n with the Google Cloud CLI\n already installed and with values already set for\n your current project. It can take a few seconds for the session to initialize.\n2. Run the [`gcloud managed-kafka topics delete`](/sdk/gcloud/reference/managed-kafka/topics/delete) command:\n\n \u003cbr /\u003e\n\n ```bash\n gcloud managed-kafka topics delete TOPIC_ID \\\n --cluster=CLUSTER_ID \\\n --location=LOCATION_ID\n ```\n\n This command removes the specified topic from the designated\n Google Cloud Managed Service for Apache Kafka cluster. All data associated with the topic is deleted,\n and the topic is longer accessible to producers or consumers.\n\n \u003cbr /\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eTOPIC_ID\u003c/var\u003e: The ID of the topic to delete.\n\n - \u003cvar translate=\"no\"\u003eCLUSTER_ID\u003c/var\u003e: The ID of the cluster where the topic is located.\n\n - \u003cvar translate=\"no\"\u003eLOCATION_ID\u003c/var\u003e: The location of the cluster.\n\n### Go\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 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 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\n\u003cbr /\u003e\n\nWhat's next?\n------------\n\n- [Monitor your topic](/managed-service-for-apache-kafka/docs/monitor-cluster)\n\n- [View a consumer group](/managed-service-for-apache-kafka/docs/view-consumer-group).\n\n- [List all consumer groups of a cluster](/managed-service-for-apache-kafka/docs/list-consumer-groups).\n\n- [Delete a consumer group](/managed-service-for-apache-kafka/docs/delete-consumer-group)."]]