Manually delete Apache Kafka topics

Using Zookeeper CLI

In the last few versions of Apache’s Kafka, deleting a topic is fairly easy. You just need to set one property in the configuration to ‘true’, and just issue a command to delete a topic. It’ll be deleted in no time. But sometimes, for several reasons unknown to mere mortals such as ourselves, the deletion of a topic doesn’t happen automatically. If this is happening to you, don’t sweat just yet; there’s another easy way to delete a topic.

First, let’s see how to configure Kafka to delete a topic with just a command. ‘cd’ into your Kafka installation directory, then into the ‘config’ directory. Here, you’ll find a server.properties file (the file name could be different if you’ve renamed your copy). Open the properties file in your favorite text editor, for me it’s Vim. Add the following line, or change the value of the property to true:

delete.topic.enable=true

Now go to the ‘bin’ directory, where you’ll find a file named ‘kafka-topics.sh.’ This is the file we’ll be using to delete a topic. The command to delete a topic is this:

./kafka-topics.sh — zookeeper localhost:2181 — delete — topic <topic_name>

Manually delete a topic with Zookeeper

After you issue the delete command, the topic will be “marked for deletion,’ and you’ll have to wait till it gets deleted. Sometimes, it doesn’t happen. When you face this problem, you can use Zookeeper to delete a topic. First, log into the Zookeeper CLI console using the proper ‘zkCli.sh’ file, you’ll find this in the ‘bin’ directory in your Zookeeper installation.

Once you’re in, make sure the topic is not deleted by issuing the following command:

get /brokers/topics/<topic_name>

If you don’t get an error, it means that topic is not yet deleted. Now run the following two commands to delete the topic completely from the system:

rmr /brokers/topics/<topic_name>
rmr /admin/delete_topics/<topic_name>

Now if you try to ‘get’ the topic using the previous command, it’ll throw an error. And that’s it, you’re done. The topic is now deleted.