apache-kafkakafka-topic

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

  • 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 --bootstrap-server localhost:9092 \
      --delete --topic 'giorgos-.*'
    

    Using the Admin APIs, you can also delete several topics at once, see AdminClient.deleteTopics