Kafka How to delete records from a topic using Java API?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a popular open-source stream-processing software platform developed by the Apache Software Foundation, written in Scala and Java. Designed to provide a unified, high-throughput, low-latency platform for handling real-time data feeds, Kafka is widely used in enterprise environments. One of its common needs is managing stored data, specifically deleting records from a Kafka topic.
Understanding Kafka Topic and Record Management
In Kafka, records (messages) are grouped in topics. In essence, a topic is a category or feed name to which records are published. Topics in Kafka are multi-subscriber, and they can have zero or many consumers that subscribe to the data written to them. The data within a topic is divided into partitions, which are distributed across multiple servers in the Kafka cluster to ensure redundancy and fault tolerance.
Why Delete Records?
Here are several reasons why one might want to delete records from a Kafka topic:
- Data retention policies: Organizations often have policies on how long data can be retained for compliance purposes.
- Disk space management: Removing obsolete or unnecessary data can help in managing disk space.
- Data correction: Removing incorrect or outdated information to maintain data integrity.
Record Deletion in Kafka
Kafka supports record deletion through two primary mechanisms:
- Retention Policy: Kafka topics are configured with a retention policy that automatically deletes records older than a specified retention period or when the log reaches a certain size.
- Tombstone Messages: For key-based topics, sending a message with a null value (tombstone) for a specific key marks that record for deletion.
1. Retention Policy Configuration
Each Kafka topic can be configured with retention settings that define how long records should be kept. The two primary configurations are:
retention.ms: Controls how long records are retained based on time.retention.bytes: Controls the maximum size of the log before older records are deleted.
These settings can be adjusted at the topic level using the Kafka administrative client.
2. Deleting records using Tombstone messages
For key-based topics, records can be explicitly marked for deletion using a tombstone message. This is a record with the same key as the record to be deleted but with a null value. Kafka uses these tombstone messages to know that all records with that key should be removed upon log compaction.
Implementing Record Deletion Using Java
To manage Kafka topics and deletion using Java, you can use the Kafka AdminClient API for modifying topic settings, and the Producer API for sending tombstone messages. Below is an example demonstrating both methods.
Kafka AdminClient API Example
Producer API for Sending Tombstone Messages
Summary Table
| Feature | Description | Use Case |
| Retention Policy | Auto-deletes records based on time or size configurations. | Ideal for general data retention management and compliance. |
| Tombstone Messages | Marks records with specific keys for deletion upon log compaction. | Useful for selectively deleting records with a certain key. |
Conclusion
Kafka provides flexible mechanisms for deleting records, either through automated retention policies or manually through tombstone messages. Using the Java API, developers can manage these functions programmatically, integrating Kafka's data management features within larger applications or systems.

