'How to delete multiple topics in Apache Kafka
Assuming that I have a number of topics with the same prefix, e.g:
giorgos-topic1
giorgos-topic2
giorgos-topic3
...
The command used for deleting a single topic (say giorgos-topic1) is the following:
./bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic giorgos-topic1
Is it possible to delete multiple topics using a single command and possibly a regular expression/wildcard (e.g. giorgos-*) instead of typing all the topic names that need to be deleted one by one?
Solution 1:[1]
Yes you can use regex-like expressions when deleting topics with the kafka-topics.sh tool:
For example, to delete all topics starting with giorgos-:
./bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic 'giorgos-.*'
Using the Admin APIs, you can also delete several topics at once, see AdminClient.deleteTopics
Solution 2:[2]
In cases where regex is not possible we can use a comma seperated list of topic names for the deletion of topics.
kafka-topics.sh --zookeeper 10.0.0.160:2181 --delete --topic giorgos-topic1,giorgos-topic2,giorgos-topic3,...
Solution 3:[3]
Just to add to the accepted answer, the * wildcard needs to be preceded by '.'
In my experience:
--delete --topic '_confluent-controlcenter-5-3-1-1-.*' works
--delete --topic '_confluent-controlcenter-5-3-1-1-*' DOESNT work
Note: I would've added this as a comment but don't have enough rep.
Solution 4:[4]
Please add single quote ('giorgos-.* ') if it complains like no matches found: giorgos-.*
./bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic 'giorgos-.*'
Solution 5:[5]
Recent Kafka versions will remove the Zookeeper dependency and going forward you need to reference the brokers, through --boostrap-server:
kafka-topics \
--bootstrap-server localhost:9092,localhost:9093,localhost:9094
--delete \
--topic 'giorgos-.*'
Solution 6:[6]
This is the easiest way to delete all the topics in one go. Follow the below steps:
- Connect to zookeeper CLI.
zookeeper-shell.bat localhost:2181
You will get a message - "Welcome to zookeeper!"
- Then type the below command and all the topics will get deleted.
deleteall /brokers/topics
I have tested this myself and it worked for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexander Popov |
| Solution 2 | Aman Saurav |
| Solution 3 | famen |
| Solution 4 | kundan bora |
| Solution 5 | Giorgos Myrianthous |
| Solution 6 | anurag tripathi |
