Scrivio Interview library
DebuggingFoundations6 min read · updated 13 November 2026

A low-traffic topic has retention.ms=86400000 (one day), but kafka-dump-log.sh shows records three days old still on disk.

Why are they not deleted, and how do you fix it?

Retention deletes whole closed segments, never single records and never the active segment. A segment rolls only when it reaches segment.bytes (1 GiB by default) or segment.ms (7 days by default), so on a low-traffic topic the active segment stays open for days and its three-day-old records cannot be deleted. Once a segment is closed it is removed when its newest record is older than retention.ms, checked every log.retention.check.interval.ms (5 minutes). The fix is to set segment.ms on that topic well below the retention, for example a few hours.

Why this question is asked

This question comes up whenever someone looks at a topic's disk usage or, more seriously, when data must be gone after a certain time for privacy or contractual reasons. The behaviour looks like a bug, retention.ms says one day and the data is three days old, but it follows directly from how Kafka stores logs. Interviewers ask it to see whether we know that retention works on segments. In this answer, we will recap segments, explain how retention decides what to delete, show why the active segment holds the old records, apply the fix, and look at retention.bytes, which follows the same rule.

1A Quick Recap of Segments

Each partition's log is stored as a sequence of segments, each a .log file with its index files, named by the offset of its first record. Only the newest segment, the active segment, receives new records; the older ones are closed and never change.

A new segment is started, which Kafka calls rolling, when the active segment reaches segment.bytes, 1 GiB by default, or when it has been open for segment.ms, 7 days by default, measured from the first record's timestamp in it. The roll happens when the next record arrives and the broker sees that a limit has been passed.

2How Retention Decides What to Delete

Every log.retention.check.interval.ms, 5 minutes by default, the broker checks each partition. For time-based retention it walks the closed segments from oldest to newest and asks: is the largest timestamp in this segment older than now minus retention.ms? If so, every record in it is past retention, and the whole segment is deleted. The check stops at the first segment that is still too young, because later segments are younger still.

Notice the two details. Deletion is per segment, so a segment with one fresh record keeps all its old records alive. And the check uses the segment's newest record, not its oldest, so a segment lives until everything in it has expired. The active segment is never a candidate at all.

Step throughTime-based retention for one partition
  1. Every 5 minutes: take the oldest segment
  2. Is it the active segment?
    YesStop: never deletedNoCheck its newest timestamp
  3. Newest record older than retention.ms?
    YesDelete the whole segment, take the nextNoStop: keep it and all newer ones

3 steps in order. Play them, or step through them one at a time.

Retention never looks inside a segment and never touches the active one.

3Why the Three-Day-Old Records Are Still There

Our topic receives only a little data, a few megabytes a day. At that rate the active segment will take years to reach 1 GiB, so the size limit never triggers a roll. The time limit, segment.ms of 7 days, will roll it eventually, but until then every record written in the last up to 7 days sits in the same active segment. Retention cannot touch it, so the three-day-old records stay, and with the defaults a record on a quiet topic can survive up to about 8 days, 7 days in the active segment plus up to one day of retention once it closes.

kafka-dump-log.sh confirms it: the old records are in the segment with the highest base offset, the active one.

Shell
$ ls /var/kafka/data/audit-events-0/*.log
00000000000000000000.log # the only segment: also the active one
$ kafka-dump-log.sh --files 00000000000000000000.log | head -3
baseOffset: 0 lastOffset: 4 count: 5 ... CreateTime: 1731054000000 # 3 days ago
baseOffset: 5 lastOffset: 9 count: 5 ... CreateTime: 1731062000000

4The Fix: Roll Segments More Often

We make the segments roll on time, well before the retention period, by setting segment.ms on that topic. With segment.ms of, say, 6 hours, a new segment starts every 6 hours (when a record arrives after that point), so each closed segment covers at most 6 hours of data. A segment is deleted once its newest record is a day old, so data is removed between about one day and one day plus 6 hours after it was written, plus up to 5 minutes for the check.

Java
kafka-configs.sh --bootstrap-server b1:9092 --alter --entity-type topics \
--entity-name audit-events --add-config segment.ms=21600000 # 6 hours
# after the next record arrives, the old segment closes;
# within one retention check its records older than 1 day can go

Notice that we change it per topic, not cluster-wide. Very small segments on busy topics create many files and lots of rolling work, so the value should be chosen relative to that topic's retention, typically a tenth to a quarter of it, not as small as possible. Also, a partition that receives no records at all does not roll, because rolling happens on append; a fully idle partition keeps its last segment until something is written.

5Retention.bytes Works the Same Way

Some topics also set retention.bytes, a size limit. Two things surprise people about it. It applies per partition, not per topic, so a topic with 12 partitions and retention.bytes of 10 GB can hold up to about 120 GB. And it also deletes only whole closed segments: when a partition's total size exceeds the limit, the oldest segments are deleted until it fits, but the active segment is never removed, so a partition can sit above the limit by up to a segment's size.

When both limits are set, a segment is deleted when either says so. For compliance requirements, time-based retention with a sensible segment.ms is what gives a predictable upper bound on how long data lives.

6Follow-up questions

Does retention use the record timestamps or the file modification time?

Modern Kafka uses the largest record timestamp in the segment, taken from the time index. That matters when producers set timestamps themselves: records with timestamps far in the future can keep a segment alive much longer than expected, which is why brokers can reject such timestamps with message.timestamp.after.max.ms.

Why does Kafka not delete individual records?

Because segments are immutable files and deleting a whole file is cheap, while removing records from the middle would mean rewriting files under live readers. The design keeps writes sequential and deletion trivial, at the cost of coarse retention.

Would a smaller segment.bytes help instead?

Not on a low-traffic topic. A small size limit makes busy partitions roll often, but a quiet partition still takes a long time to fill even a small segment. segment.ms rolls by time, which is what time-based retention needs.

7Conclusion

In this answer, we saw that retention deletes only whole closed segments whose newest record is older than retention.ms, checked every 5 minutes, and never the active segment. On a low-traffic topic the active segment can stay open for up to the default segment.ms of 7 days, so records much older than one day survive. Setting segment.ms on the topic to a fraction of the retention, such as 6 hours, makes segments close and become eligible, and retention.bytes follows the same whole-segment rule, per partition.

What to say in the interviewAbout 90 seconds aloud

Open with

The records are not deleted because retention works on whole segments, not on individual records, and it never deletes the active segment that is still being written. A segment only rolls when it hits segment.bytes, which is 1 GiB by default, or segment.ms, which is 7 days by default.

Then, if they want more

On a low-traffic topic the size limit never triggers, so the active segment stays open for up to a week, and all those records, including the three-day-old ones, are in it and cannot be touched.

Once a segment is closed, the broker deletes it when the newest record in it is older than retention.ms, and it checks every log.retention.check.interval.ms, 5 minutes by default. The fix is to set segment.ms on that topic well below the retention, for example 6 hours for one day of retention.

Then segments close every 6 hours, as long as records keep arriving, and data is gone between one day and about a day and 6 hours after it was written. I would set it per topic and not too small, because tiny segments on busy topics create lots of files.

I would also mention that retention.bytes, if it is set, applies per partition rather than per topic, and also only removes whole closed segments, so a partition can sit above it by up to one segment.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why are they not deleted, and how do you fix it?": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/4c2aedb84e/