119

I need to delete a topic in Kafka 0.8.2.2.3. I have used the below command for deleting the topic:

bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic DummyTopic

The command executed successfully, but when I run a command to list the topics, I could see that the topic is still there and it shows marked for deletion.

bin/kafka-topics.sh --list --zookeeper localhost:2181
DummyTopic - marked for deletion

And when I create the topic DummyTopic, it outputs the exception. The topic already exists, and below is the stack trace:

Error while executing topic command Topic "DummyTopic" already exists.
kafka.common.TopicExistsException: Topic "DummyTopic" already exists.
    at kafka.admin.AdminUtils$.createOrUpdateTopicPartitionAssignmentPathInZK(AdminUtils.scala:248)
    at kafka.admin.AdminUtils$.createTopic(AdminUtils.scala:233)
    at kafka.admin.TopicCommand$.createTopic(TopicCommand.scala:92)
    at kafka.admin.TopicCommand$.main(TopicCommand.scala:54)
    at kafka.admin.TopicCommand.main(TopicCommand.scala)

How can I delete this topic?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rishi Arora
  • 1,713
  • 3
  • 16
  • 31
  • 19
    Apache Kafka never deletes a topic marked for deletion if that topic has producers still producing to it, or consumers still consuming from it, or messages left hanging out in the queue. One way to try to try to force it is to restart Kafka. Or if that doesn't work, go in under the hood and Delete the directory that is your topic name under /var/local/kafka/data and then restart Kafka then re-issue the delete command. I wish Apache Kafka had a "nuke the bleeping topic" option so the developer can issue the command: "Really, totes for real this time nuke the bleeping topic no sass please". – Eric Leschinski Sep 07 '16 at 21:07
  • 6
    bin/kafka-topics.sh –delete –zookeeper localhost:2181 –topic – Avinav Mishra Feb 21 '17 at 02:16
  • Option zookeeper is deprecated, use --bootstrap-server instead. https://stackoverflow.com/a/53429129/5456789 – Amir Azizkhani Jul 06 '22 at 10:40
  • This worked for me (within docker): bin/kafka-topics --delete --topic DummyTopic --bootstrap-server localhost:29092 – Enrico Giurin Nov 28 '22 at 03:46

1 Answers1

139

Deletion of a topic has been supported since 0.8.2.x version. You have to enable topic deletion (setting delete.topic.enable to true) on all brokers first.

Note: Ever since 1.0.x, the functionality being stable, delete.topic.enable is by default true.

Follow this step by step process for manual deletion of topics

  1. Stop Kafka server
  2. Delete the topic directory, on each broker (as defined in the logs.dirs and log.dir properties) with rm -rf command
  3. Connect to Zookeeper instance: zookeeper-shell.sh host:port
  4. From within the Zookeeper instance:
    1. List the topics using: ls /brokers/topics
    2. Remove the topic folder from ZooKeeper using: rmr /brokers/topics/yourtopic
    3. Exit the Zookeeper instance (Ctrl+C)
  5. Restart Kafka server
  6. Confirm if it was deleted or not by using this command kafka-topics.sh --list --zookeeper host:port
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211