system design
delete
kafka
topic

Is there a way to delete all the data from a topic or delete the topic before every run?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, you can clear a Kafka topic, but the safest method depends on why you want a fresh start. In development and tests, deleting and recreating the topic is often the cleanest approach. In production, changing retention or deleting records selectively is usually safer than destroying the topic itself.

Delete and Recreate the Topic in Test Environments

If this is a disposable topic used in integration tests or repeated local runs, delete and recreate it.

bash
1bin/kafka-topics.sh --bootstrap-server localhost:9092 \
2  --delete --topic demo-topic
3
4bin/kafka-topics.sh --bootstrap-server localhost:9092 \
5  --create --topic demo-topic \
6  --partitions 3 \
7  --replication-factor 1

This gives you a genuinely empty topic with fresh partitions. It is often the simplest workflow when your test environment fully controls Kafka.

The downside is that clients depending on the topic must tolerate the topic disappearing and reappearing.

Use Retention or Delete Records When the Topic Must Stay

If you want the topic to continue existing, you have other options.

One approach is to lower retention temporarily:

bash
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
  --alter --entity-type topics --entity-name demo-topic \
  --add-config retention.ms=1000

After cleanup, restore the intended retention:

bash
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
  --alter --entity-type topics --entity-name demo-topic \
  --add-config retention.ms=604800000

Another option is record deletion up to specific offsets with kafka-delete-records.sh, which is more surgical than deleting the whole topic.

Understand What Does Not Delete Data

Resetting consumer offsets does not delete topic data. It only changes where a consumer group reads next.

Similarly, consuming all messages does not delete them from Kafka. Messages remain until retention or compaction removes them.

That distinction matters a lot:

  • offset reset changes read position
  • retention changes lifecycle
  • topic deletion destroys the topic itself

These are different operations and should not be confused.

Programmatic Reset with AdminClient

If you want the topic reset inside test code, the Admin API is a good fit.

java
1import java.util.List;
2import java.util.Properties;
3import org.apache.kafka.clients.admin.AdminClient;
4import org.apache.kafka.clients.admin.AdminClientConfig;
5import org.apache.kafka.clients.admin.NewTopic;
6
7public class TopicReset {
8    public static void main(String[] args) throws Exception {
9        Properties props = new Properties();
10        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
11
12        try (AdminClient admin = AdminClient.create(props)) {
13            admin.deleteTopics(List.of("demo-topic")).all().get();
14            admin.createTopics(List.of(new NewTopic("demo-topic", 3, (short) 1))).all().get();
15        }
16    }
17}

This is a common approach in automated tests where a clean topic state is part of the fixture setup.

What to Avoid

Manually deleting broker log directories should not be the default answer. It bypasses the normal Kafka admin path and is easy to get wrong, especially in shared or production clusters.

Use broker filesystem deletion only as a low-level recovery step when you fully understand the cluster state and have exhausted the supported administrative options.

Common Pitfalls

The biggest mistake is assuming "read everything" or "reset offsets" empties the topic. It does not. The messages are still in Kafka.

Another issue is deleting and recreating a topic in an environment where other consumers or stream processors assume the topic always exists. That can cause noisy failures unrelated to the data-reset goal.

Developers also sometimes set retention to zero and expect immediate deletion with perfect timing. Kafka cleanup is policy-driven and asynchronous, so you should not rely on sub-second precision for production logic.

Finally, avoid topic deletion as a routine production operation unless your design truly expects it. In most production systems, retention settings and consumer offset control are safer tools.

Summary

  • In test environments, deleting and recreating the topic is often the simplest clean reset.
  • In longer-lived environments, retention changes or delete-records operations are usually safer.
  • Resetting consumer offsets does not delete topic data.
  • Manually deleting broker files is not the normal or recommended reset path.
  • Pick the method that matches your environment: disposable test topic versus persistent production topic.

Course illustration
Course illustration

All Rights Reserved.