Delete a Google Cloud Managed Service for Apache Kafka topic

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, see Manage 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 the Required permissions section:

Required permissions

The following permissions are required to delete a topic:

  • Delete a topic: managedkafka.topics.delete

You might also be able to get these permissions with custom roles or other predefined roles.

For more information about this role, see Managed Service for Apache Kafka predefined roles.

Delete a topic

Deleting a topic is irreversible and results in the permanent loss of 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

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

    Go to Clusters

  2. 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, for the Resources tab, the topics are listed.

  3. Click the topic that you want to delete.

    The Topic details page opens.

  4. Click Delete and confirm the operation.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session 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.

  2. Run the gcloud managed-kafka topics delete command:

    gcloud managed-kafka topics delete TOPIC_ID \
        --cluster=CLUSTER_ID \
        --location=LOCATION_ID

    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.

    • LOCATION_ID: The location of the cluster.

Go

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
	"google.golang.org/api/option"

	managedkafka "cloud.google.com/go/managedkafka/apiv1"
)

func deleteTopic(w io.Writer, projectID, region, clusterID, topicID string, opts ...option.ClientOption) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// clusterID := "my-cluster"
	// topicID := "my-topic"
	ctx := context.Background()
	client, err := managedkafka.NewClient(ctx, opts...)
	if err != nil {
		return fmt.Errorf("managedkafka.NewClient got err: %w", err)
	}
	defer client.Close()

	clusterPath := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", projectID, region, clusterID)
	topicPath := fmt.Sprintf("%s/topics/%s", clusterPath, topicID)
	req := &managedkafkapb.DeleteTopicRequest{
		Name: topicPath,
	}
	if err := client.DeleteTopic(ctx, req); err != nil {
		return fmt.Errorf("client.DeleteTopic got err: %w", err)
	}
	fmt.Fprint(w, "Deleted topic\n")
	return nil
}

Java

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
import com.google.cloud.managedkafka.v1.TopicName;
import java.io.IOException;

public class DeleteTopic {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the example.
    String projectId = "my-project-id";
    String region = "my-region"; // e.g. us-east1
    String clusterId = "my-cluster";
    String topicId = "my-topic";
    deleteTopic(projectId, region, clusterId, topicId);
  }

  public static void deleteTopic(String projectId, String region, String clusterId, String topicId)
      throws Exception {
    try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create()) {
      // This operation is being handled synchronously.
      managedKafkaClient.deleteTopic(TopicName.of(projectId, region, clusterId, topicId));
      System.out.println("Deleted topic");
    } catch (IOException | ApiException e) {
      System.err.printf("managedKafkaClient.deleteTopic got err: %s", e.getMessage());
    }
  }
}

Python

from google.api_core.exceptions import NotFound
from google.cloud import managedkafka_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")
except NotFound as e:
    print(f"Failed to delete topic {topic_id} with error: {e.message}")

What's next?