In this tutorial, you will learn how to delete Kafka topics using the Kafka Command-line Interface(CLI).
Before deleting a Kafka topic, there are a few things you should keep in mind:
- 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.
- 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.
- 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.
- 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.
- Ensure that
delete.topic.enableis set to true: Before deleting a topic, make sure that
delete.topic.enableis 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.
- Open a terminal window and navigate to the directory where Kafka is installed.
- Run the following command to delete the topic:
./bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic <topic_name>
- <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.
I hope this tutorial was helpful to you. To learn more about Kafka, please check my other Kafka tutorials for beginners.