How do I delete/clean Kafka queued messages without deleting Topic
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform known for its high-throughput, durability, and scalability. It is commonly used to handle real-time data feeds in a variety of systems. Kafka works by storing records in topics which are divided into partitions. These records within topics can pile up over time, and you might need to clean up or delete these records without deleting the entire topic. This can be necessary for operational reasons like reclaiming disk space or for compliance requirements.
How Kafka Manages Data
To understand how to delete or clean Kafka queued messages, it's important to know how Kafka handles data. Kafka topics are multi-partition; each partition can be seen as an ordered, immutable sequence of records that is continually appended to—a commit log. Records in each partition are assigned a sequential id number called the offset. Kafka retains all messages for a set amount of time (configurable), and the data is stored in log files on the disk.
Key Concepts for Deleting Messages
Kafka does not support deleting individual records out of the box. The primary mechanisms provided by Kafka to manage the lifecycle of messages are retention policies and log compaction.
1. Retention Policies:
Kafka allows configuring time-based and size-based retention policies for logs at the topic level:
- Time-based Retention (retention.ms): This setting determines how long Kafka messages are kept before being automatically deleted. For example, setting
retention.ms=3600000would mean messages are retained for one hour. - Size-based Retention (retention.bytes): This policy limits the total size of log data retained on the disk. Once the size exceeds the configured threshold, the oldest log files are deleted to stay within the limit.
2. Log Compaction:
Log compaction ensures that Kafka retains at least the last known value for each key within a partition. It is particularly useful for topics that represent database change logs or current state representation where only the latest update is relevant.
How to Configure Retention Policies
Configuring the retention policy for a Kafka topic can be done either at the time of topic creation or by altering the settings on an existing topic. Here's how you can alter an existing topic to change its retention policy:
Cleaning up Kafka Queued Messages
If you decide to clean up queued messages based on the adjusted retention policy immediately, you can manually trigger a log cleanup:
Summary Table
| Configuration | Description | Example Setting | Impact |
| retention.ms | Time after which messages will be deleted | 172800000 (2 days) | Messages older than specified time will be deleted, freeing up disk space. |
| retention.bytes | Max size of logs on disk | 1073741824 (1GB) | Old messages are deleted to ensure log doesn’t exceed specified size. |
| Log Compaction | Retains last message for each key | NA | Useful for topics representing state where only the latest state per key is relevant. |
| Manual Cleanup Trigger | Command to expedite cleanup process | kafka-topics --execute-log-compaction | Forces immediate cleanup based on current log cleaner settings. Useful for immediate space recovery. |
Additional Points to Consider
- Consumer Offsets: Adjusting retention settings can affect consumers, as they might lose access to older messages they haven't consumed yet. Ensure consumer offsets are managed properly.
- Replication and Fault Tolerance: When adjusting topic settings, ensure that replication factors and other fault tolerance mechanisms remain unaffected to ensure high availability.
By understanding and utilizing Kafka's data management features like retention policies and log compaction, you can efficiently manage the storage of messages on Kafka without needing to delete entire topics, thus maintaining smooth operations and compliance with data retention policies.
Related reading
- How do I do async RPC calls with RabbitMq
- How do I ensure that logs are retained forever in Kafka?
- How do I ensure that only one consumer actually consumes a published message?
- How do I handle RabbitMQ Consumer Cancellation Notification when using Spring ChannelAwareMessageListener
- How do i implement Headers Exchange in RabbitMQ using Java?
- How do I implement in memory or embedded kafka not for testing purposes?
- How do I implement Kafka Consumer in Scala
- How do I initialize a CuratorFramework for a ZooKeeper cluster with dynamic size?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.