117

I need to delete the topic test in Apache Kafka 0.8.1.1.

As expressed in the documentation here, I have executed:

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

However, this results in the following message:

Command must include exactly one action: --list, --describe, --create or --alter

How can I delete this topic?

EmPak5
  • 1,205
  • 2
  • 10
  • 11
  • Step by step procedure is explained in : https://stackoverflow.com/questions/33537950/how-to-delete-a-topic-in-apache-kafka – Ravindra babu Jul 24 '19 at 10:44

18 Answers18

130

Deleting topic isn't always working in 0.8.1.1

Deletion should be working in the next release, 0.8.2

kafka-topics.sh --delete --zookeeper localhost:2181 --topic your_topic_name

  Topic your_topic_name is marked for deletion.
  Note: This will have no impact if delete.topic.enable is not set to true.

You may also pass in a bootstrap server instead of zookeeper:

kafka-topics.sh --bootstrap-server kafka:9092 --delete --topic your_topic_name

Is it possible to delete a topic?

Jira KAFKA-1397

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
frank
  • 1,761
  • 2
  • 16
  • 20
  • Thanks for the info. Do you know how to clear the entire Kafka and Zookeeper states as indicated? – EmPak5 Jun 18 '14 at 16:09
  • 1
    @EmPack IIRC, the easiest way to do that (if you don't care about up time) is to shutdown all your brokers, delete your logs for that topic and changing you ZK namespace. – frank Jun 18 '14 at 16:16
96

It seems that the deletion command was not officially documented in Kafka 0.8.1.x because of a known bug (https://issues.apache.org/jira/browse/KAFKA-1397).

Nevertheless, the command was still shipped in the code and can be executed as:

bin/kafka-run-class.sh kafka.admin.DeleteTopicCommand --zookeeper localhost:2181 --topic test

In the meantime, the bug got fixed and the deletion command is now officially available from Kafka 0.8.2.0 as:

bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic test
Andrea
  • 3,627
  • 4
  • 24
  • 36
  • From what I read, it might work 0.8.1.1, but it might not... The fix is also in the trunk since a while, so if you took the sources directly from the trunk, it should work – frank Sep 05 '14 at 13:56
  • I've just tried on 0.8.1.1, it reports "deletion succeeded" but it didn't work. Topic is still there, I can attach a consumer and it will report all previous items as pending. – Elias Dorneles Jan 27 '15 at 12:37
  • Deleting topics in Kafka 0.8.1.1 does not work. The recently released version 0.8.2.0 should finally have proper deletion support. See http://blog.confluent.io/2014/12/02/whats-coming-in-apache-kafka-0-8-2/. – miguno Feb 19 '15 at 13:01
  • BTW, the property for delete.topic.enable=true can be located in the server.properties file. It is commented out by default. – Shanemeister Mar 10 '17 at 01:34
40

Add below line in ${kafka_home}/config/server.properties

delete.topic.enable=true

Restart the kafka server with new config:

${kafka_home}/bin/kafka-server-start.sh ~/kafka/config/server.properties

Delete the topics you wish to:

${kafka_home}/bin/kafka-topics.sh --delete  --zookeeper localhost:2181  --topic daemon12
PradeepKumbhar
  • 3,361
  • 1
  • 18
  • 31
10

Andrea is correct. we can do it using command line.

And we still can program it, by

ZkClient zkClient = new ZkClient("localhost:2181", 10000);
zkClient.deleteRecursive(ZkUtils.getTopicPath("test2"));

Actually I do not recommend you delete topic on Kafka 0.8.1.1. I can delete this topic by this method, but if you check log for zookeeper, deletion mess it up.

Liang
  • 361
  • 3
  • 9
  • Should it also include zkClient.deleteRecursive(ZkUtils.getTopicConfigPath("test2")); and – Sumit Dec 11 '14 at 20:56
  • @Sumit, and what? I did not use that "getTopicConfigPath". Actually I do not recommend you delete topic on Kafka 0.8.1.1. I can delete this topic by this method, but if you check log for zookeeper, deletion mess it up. – Liang Dec 12 '14 at 22:06
  • but this is how kafka also does it (the way you suggested): https://apache.googlesource.com/kafka/+/0.8.1/core/src/main/scala/kafka/admin/DeleteTopicCommand.scala – Sumit Dec 14 '14 at 00:21
  • @Sumit. yes, I found the code from there. I am not sure about that "messy up" thing. Because once I delete a topic on my company server, the deletion generate lots of junk to zookeeper log. But I can not reproduce it in my local. So I do not recommend that.. – Liang Dec 15 '14 at 18:45
5

Steps to Delete 1 or more Topics in Kafka

#To delete topics in kafka the delete option needs to be enabled in Kafka server. 1. Go to {kafka_home}/config/server.properties 2. Uncomment delete.topic.enable=true

#Delete one Topic in Kafka enter the following command

kafka-topics.sh --delete --zookeeper localhost:2181 --topic <your_topic_name>

#To Delete more than one topic from kafka (good for testing purposes, where i created multiple topics & had to delete them for different scenarios)

  1. Stop the Kafka Server and Zookeeper
  2. go to server folder where the logs are stored (defined in their config files) and delete the kafkalogs and zookeeper folder manually
  3. Restart the zookeeper and kafka server and try to list topics,

bin/kafka-topics.sh --list --zookeeper localhost:2181

if no topics are listed then the all topics have been deleted successfully.If topics are listed, then the delete was not successful. Try the above steps again or restart your computer.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
CodeUrLife
  • 391
  • 4
  • 5
  • 1
    Note that for the latest Kafka version at the moment (1.0.0) that option doesn't appears in the ``server.properties`` file. You have to add that line manually. – David Corral Dec 18 '17 at 10:08
  • 1
    @DavidCorral topic deletion is enabled by default in 1.0.0 – Tanner Jan 05 '18 at 20:20
4

You can delete a specific kafka topic (example: test) from zookeeper shell command (zookeeper-shell.sh). Use the below command to delete the topic

rmr {path of the topic}

example:

rmr /brokers/topics/test
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
Yahya
  • 53
  • 1
  • 5
3

This steps will delete all topics and data

  • Stop Kafka-server and Zookeeper-server
  • Remove the data directories of both services, by default on windows they are C:/tmp/kafka-logs and C:/tmp/zookeeper.
  • then start Zookeeper-server and Kafka-server
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Venkatesh Boya
  • 564
  • 5
  • 11
2

As mentioned in doc here

Topic deletion option is disabled by default. To enable it set the server config delete.topic.enable=true Kafka does not currently support reducing the number of partitions for a topic or changing the replication factor.

Make sure delete.topic.enable=true

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
abmd
  • 191
  • 1
  • 6
2

The command:

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

unfortunately only marks topic for deletion.

Deletion does not happen.

That makes troubles, while testing any scripts, which prepares Kafka configuration.

Connected threads:

Community
  • 1
  • 1
1

Adding to above answers one has to delete the meta data associated with that topic in zookeeper consumer offset path.

bin/zookeeper-shell.sh zookeeperhost:port

rmr /consumers/<sample-consumer-1>/offsets/<deleted-topic>

Otherwise the lag will be negative in kafka-monitoring tools based on zookeeper.

Knight71
  • 2,927
  • 5
  • 37
  • 63
1

First, you run this command to delete your topic:

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

List active topics to check delete completely:

$ bin/kafka-topics.sh --list --bootstrap-server localhost:9092
yuen26
  • 871
  • 11
  • 12
1

This worked for me:

kafka-topics --delete --bootstrap-server localhost:9092 --topic user-commands

I am using Confluent CLI and run this on the directory I installed it.

Laura Corssac
  • 1,217
  • 1
  • 13
  • 23
0
bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic <topic-name>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Avinav Mishra
  • 718
  • 9
  • 12
  • This **marks** a topic for deletion, not blocks and waits for deletion – OneCricketeer Sep 29 '21 at 13:03
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Sep 30 '21 at 06:52
0

If you have issues deleting the topics, try to delete the topic using:

$KAFKA_HOME/bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic your_topic_name

command. Then in order to verify the deletion process, go to the kafka logs directory which normally is placed under /tmp/kafka-logs/, then delete the your_topic_name file via rm -rf your_topic_name command.

Remember to monitor the whole process via a kafka management tool like Kafka Tool.

The mentioned process above will remove the topics without kafka server restart.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Vahid F
  • 377
  • 1
  • 7
  • 21
0

Step 1: Make sure you are connected to zookeeper and Kafka running

Step 2: To delele the Kafka topic run kafka-topics script, add the port and --topic with name of your topic and --delete it just delete the topic with success.

# Delete the kafka topic
# it will delete the kafka topic
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --topic name_of_topic --delete
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Black_Dreams
  • 572
  • 1
  • 5
  • 11
0

There is actually a solution without touching those bin/kafka-*.sh: If you have installed kafdrop, then simply do:

url -XPOST http://your-kafdrop-domain/topic/THE-TOPIC-YOU-WANT-TO-DELETE/delete
ch271828n
  • 15,854
  • 5
  • 53
  • 88
0

Recent Kafka versions are about to remove the Zookeeper dependency. Therefore, you should instead reference the brokers (through --boostrap-server):

kafka-topics \
    --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
    --delete \
    --topic topic_for_deletion
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

for confluent cloud use -

$ confluent kafka topic delete my_topic
erptocoding
  • 305
  • 3
  • 5