How can I delete all messages from an Apache Pulsar topic?

Is there a way to use the Pulsar command line tools to delete all messages for a topic? It doesn't have any subscriptions yet and from what I can tell the tools that do this operate on a subscription. I need to get rid of the old junk before starting the service that consumes the topic.


Solution 1:

@David Tinker, Please try this delete topic command: $ pulsar-admin persistent delete persistent://test-tenant/ns1/tp1 Before execute, there should be not any active subscription or producer connected to it. If topic has subscription and producers connected, and you done care the existing data, you could add --force at the end. After delete this topic, when produce data, producer will reconnect and auto create the topic with same name.

Solution 2:

See the documentation.

List the topics with:

pulsar-admin persistent list tenant/namespace

Then to delete them:

pulsar-admin topics delete "persistent://tenant/namespace/topic

or:

pulsar-admin topics delete "persistent://tenant/namespace/topic" --force

You can check subscribers with:

pulsar-admin persistent subscriptions "persistent://tenant/namespace/topic"

You can check publishers with (publisher section of the output):

pulsar-admin topics stats "persistent://tenant/namespace/topic"

If you use the force option, beware any connected publishers might immediately recreate the topic.