Apache Kafka provides us with a powerful CLI tool that allows to manage the various components of the Kafka cluster. Creating or deleting topics is one of the most common operations that you will find yourself performing.
This post explores on how we can use the Kafka CLI tools to delete the topics from an Apache Kafka cluster.
What Is the Kafka-Topics Command?
Once you install the Apache Kafka on your machine, you will get an access to the “kafka-topics.sh” tool. This command-line utility allows the management of the topics in a Kafka cluster. This tool enables us to perform tasks such as creating topics, listing topics, and deleting existing topics from a given cluster.
We can also use this tool to display the topics such as the number of partitions, replicas, and configuration settings. The administrators and developers commonly use it to manage the topics and configuration of a Kafka cluster.
This tutorial teaches us how to use this tool to delete the topics from a cluster.
Create a Topic in Apache Kafka
Let us start by creating a sample topic for demonstration purposes. Keep in mind that this section goes into detail about the process of creating Kafka topics. You can check our tutorial on that to learn more.
Example Topic:
In this example, we create a Kafka topic called “sample_topic” with three partitions and a replication factor of 1. The target Kafka cluster is on the localhost and port 9092.
The provided command should invoke the “kafka-topics.sh” utility to create a new topic called “sample_topic”. The topic includes the specified parameter such as the replication_factor and the partitions.
Output:
Created topic sample_topic.
Once we create the target topic, we can use the –list options to show the available topics as shown in the following:
This command should return all the available topics as shown in the following:
sample_topic
Let us now proceed and discuss how we can delete the topics from a cluster.
Enable the Topic Deletion
The first step is to allow Kafka to delete the topics that are specified in a given command. By default, Kafka prevents you from accidentally deleting the existing topics by disabling this feature.
To enable the Kafka topic delete, edit the server configuration file as follows:
Navigate to the end of the configuration file and add the entry that is shown in the following:
You can now save the file and reload the Kafka service.
Delete the Topic
Once you enable the topic deletion on your cluster, you can use the “kafka-topics.sh” command followed by the –delete option to remove an existing topic from the cluster.
For example, to delete the sample_topic that we created earlier, we can use the following command:
The previous command deletes a topic named “sample_topic” in a Kafka cluster which runs on “localhost” on port 9092.
The –bootstrap-server option allows us to specify the address and port of a broker in the Kafka cluster that is used to manage the topics. The –delete option indicates that we wish to delete the topic, and the –topic option specifies the topic’s name to delete.
NOTE: Deleting a Kafka topic is a non-reversible operation. This means that once the data is deleted, it cannot be recovered.
You can verify that the topic does not exist by listing the available Kafka topics.
Conclusion
We demonstrated how to create, list, and delete the Kafka topics from a given cluster using the kafka-topics command. We hope this tutorial helped you expand your Kafka knowledge.