Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete Topics using confluent-kafka-python

I am using Kafka to make multiple micro services communicate with each other. Services are written in Python, and I use Confluent library to handle Kafka. At some point, I know that some Topics are just 'over', so that I could clean them automatically.

Is there a way to delete "Topics" thanks to Confluent library ? I cannot find any documentation on this...

Thanks

like image 535
fredczj Avatar asked Dec 11 '25 04:12

fredczj


1 Answers

You can use the confluent Admin Api's to delete a topic

Example

Takes an AdminClient instance and a list of topics

def example_delete_topics(a, topics):
    """ delete topics """

    # Call delete_topics to asynchronously delete topics, a future is returned.
    # By default this operation on the broker returns immediately while
    # topics are deleted in the background. But here we give it some time (30s)
    # to propagate in the cluster before returning.
    #
    # Returns a dict of <topic,future>.
    fs = a.delete_topics(topics, operation_timeout=30)

    # Wait for operation to finish.
    for topic, f in fs.items():
        try:
            f.result()  # The result itself is None
            print("Topic {} deleted".format(topic))
        except Exception as e:
            print("Failed to delete topic {}: {}".format(topic, e))
like image 150
Sai Kiran Avatar answered Dec 13 '25 18:12

Sai Kiran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!