Kafka
Queued Messages
Topic Deletion
Data Cleaning
Message Management

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.

Practice system design

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=3600000 would 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:

bash
# To change retention policy to 2 days
kafka-configs --bootstrap-server localhost:9092 --entity-type topics --entity-name your-topic-name --alter --add-config retention.ms=172800000

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:

bash
# To trigger a cleanup
kafka-topics --bootstrap-server localhost:9092 --topic your-topic-name --execute-log-compaction

Summary Table

ConfigurationDescriptionExample SettingImpact
retention.msTime after which messages will be deleted172800000 (2 days)Messages older than specified time will be deleted, freeing up disk space.
retention.bytesMax size of logs on disk1073741824 (1GB)Old messages are deleted to ensure log doesn’t exceed specified size.
Log CompactionRetains last message for each keyNAUseful for topics representing state where only the latest state per key is relevant.
Manual Cleanup TriggerCommand to expedite cleanup processkafka-topics --execute-log-compactionForces 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.