Click to copy

Cookbook

Is there a way to purge a topic in Apache Kafka?

Problem

You want to get rid of the content within a Kafka topic and you wonder if there is a way to purge it.

Solution

You can always reduce the retention period on the topic so that it deletes all of its contents:

bin/kafka-configs.sh \
  --bootstrap-server <bst server>:9091 \
  --entity-type topics \
  --alter \
  --entity-name <topic name> \
  --add-config retention.ms=1000

After executing this command, you just have to wait for the purge to take effect (duration depends on the size of the topic). Once purged, delete the retention.ms config:

bin/kafka-configs.sh \
  --bootstrap-server <bst server>:9091 \
  --entity-type topics \
  --alter \
  --entity-name <topic name> \
  --delete-config retention.ms

Discussion

retention.ms is a configuration that controls the maximum time the cluster will retain a log before it’s discarded. Its default value is 7 days, changing it to 1000 ms means that after just 1 second the message will be deleted if we are using the "delete" retention policy (which is the default one).

To check the actual retention.ms you can run the —describe option:

bin/kafka-configs.sh \
  --bootstrap-server <bst server>:9091 \
  --entity-type topics \
  --entity-name <topic name> \
  --describe

References

  • retention.ms in the Apache Kafka documentation
  • Default retention / cleanup policy