Skip to content
Back to Insights
Data EngineeringBy KE Engineering Team

KTools: Truncate a Kafka Topic

KTools: Truncate a Kafka TopicDATA ENGINEERING cover for KTools: Truncate a Kafka Topic0x000x1F0x3A0x570x6BDATA ENGINEERINGKTools: Truncate aKafka Topic// TRUNCATE WITHOUT DELETE

First written February 2024, last updated September 2026.

Kafka has no truncate command. Most teams delete and recreate the topic, which isn't always allowed or possible. While truncating a topic is something that should never be done in production, it is quite useful in development. With this in mind, the kafka-topic-truncate CLI was created.

Why?

This tool is more a convenience than a requirement for developer success. The goal is to let a developer reset their environment as easily as possible. Publishing Avro to a JSON topic leads to serialization exceptions. While offsets can be reset, those messages are still there and can keep tripping developers up during consumer development. Truncation allows a quick removal and restart.

Cluster Prevents Topic Deletion

Not all clusters have (or give) developers the ability to delete topics. This tool allows them to remove contents from a topic, which would otherwise be impossible.

Truncating an Active Topic

Another advantage of this tool is that it can be run against a topic that has active producers and consumers, something that can't be done when a topic is deleted and recreated.

Important Features

Requires --execute to do the truncation

This mimics the approach used by the Apache Kafka CLI, such as kafka-consumer-groups. When an action has no rollback, requiring --execute adds a small “are you sure” safeguard.

Requires --force on compact-only topics

There are two cleanup policies for a topic, delete and compact. The Admin API can't perform deleteRecords on a topic that doesn't have a delete cleanup policy. This tool will temporarily add the delete cleanup policy to the topic so the delete operation can be performed. Because of this additional action, --force is required for a topic without a delete cleanup policy.

If an exception occurs during this operation, please validate that the cleanup.policy was restored. The reset is in a finally block, but it could fail too. The flag is only required for a topic without a delete cleanup policy, so a policy of compact,delete doesn't need it.

Project

This CLI tool is the first tool to be added to the KTools open-source project. The project will have a .tar release for each version. The project is written in Java, uses the Kafka Admin API for performing the delete operation, and is licensed under the Apache License Version 2.0.

The ktools-cli submodule uses the picocli library for providing CLI operations to this project; all future CLI tools will just become a new subcommand, and scripts will be added to the bin directory to invoke the jar.

As with all code you download, please verify it is developed to your liking.

Working on something like this?

Start a Conversation