Delete Kafka Topic Tutorial

In this tutorial, you will learn how to delete Apache Kafka topics using the Kafka Command-line Interface(CLI).

If you are interested in video lessons then check my video course Apache Kafka for Event-Driven Spring Boot Microservices.

Before Deleting Kafka Topic

Before deleting a Kafka topic, there are a few things you should keep in mind:

  1. Deleting a topic is permanent: Once you delete it, it is gone forever. There is no way to undo the deletion or recover the data that was stored in the topic.
  2. Deleting a topic deletes it from all replicas: When you delete a topic, it is deleted from all replicas in your Kafka cluster. This means that the data stored in the topic will be lost permanently.
  3. Make sure you have a backup of your data: Before deleting a topic, make sure you have a backup of your data. This will allow you to restore the data if you need it later.
  4. Make sure all consumers have stopped consuming data from the topic: If there are any consumers still consuming data from the topic, they will continue to do so even after the topic has been deleted. This can cause issues with your Kafka cluster and may result in data loss.
  5. Ensure that delete.topic.enable is set to true: Before deleting a topic, make sure that delete.topic.enable is set to true in your Kafka configuration file. If this setting is not enabled, you will not be able to delete topics.

Deleting Kafka Topic

To delete a Kafka topic in a KRaft cluster, you can use the kafka-topics.sh script. It is the same script we used to create and list Kafka topics. But to delete the topic, we will use the --delete parameter instead.

Alright, so follow these steps to delete the Kafka topic.

  1. Open a terminal window and navigate to the directory where Kafka is installed.
  2. Run the following command to delete the topic:
./bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic <topic_name>

Where:

    • <topic_name> – is the name of the topic to delete. 
    • localhost:9092 – is the address of the bootstrap server in your Kafka cluster. The topic will not be deleted if this server is down or unhealthy. This is why, if your Kafka cluster has more than one broker, it is recommended to provide at least two broker addresses here. When providing multiple addresses, separate them by comma. For example: --bootstrap-server localhost:9092, localhost:9094.

And this is it. If the topic’s name is correct and the provided bootstrap address is healthy, the topic will be deleted from your Kafka cluster.

Delete Multiple Kafka Topics

You can also delete multiple Apache Kafka topics at once. But be careful with that. If you make a mistake, you can accidentally delete topics you did not intend to delete.

To delete multiple Kafka topics in a cluster started with KRaft, you can use the same kafka-topics.sh script.

./bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic '<topic_pattern>'

Replace <topic_pattern> with a regular expression that matches the names of the topics you want to delete. For example, if you want to delete all topics that start with users_, you can use the following command:

./bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic 'users_.*'

This will delete all topics that start with users_.

Final words

I hope this tutorial was helpful to you. To learn more about Kafka, please check my other Kafka tutorials for beginners.

Happy learning!