5 Ways to Boost Apache Kafka Data Retention Period

Updated

apache kafka

To extend retention on one topic, set retention.ms with kafka-configs.sh — it takes effect immediately, no restart:

$ bin/kafka-configs.sh --bootstrap-server localhost:9092 \
    --entity-type topics --entity-name my_topic \
    --alter --add-config retention.ms=2592000000        # 30 days
Completed updating config for topic my_topic.

To change it for every topic at once, set log.retention.ms as a cluster-wide broker default — also live. Do not reach for log.retention.hours, which is where most guides send you: it is read-only and can only be changed by editing server.properties and restarting every broker.

The setting most guides tell you to change is the one you can’t

Increasing log.retention.hours from 168 to 720 to extend retention to 30 days is rejected by a running Kafka 4.3.1 cluster:

$ bin/kafka-configs.sh --bootstrap-server localhost:19092 --entity-type brokers \
    --entity-default --alter --add-config log.retention.hours=720
org.apache.kafka.common.errors.InvalidRequestException:
    Cannot update these configs dynamically: [log.retention.hours]

$ bin/kafka-configs.sh --bootstrap-server localhost:19092 --entity-type brokers \
    --entity-default --alter --add-config log.retention.ms=2592000000
Completed updating default config for brokers in the cluster.

Kafka’s generated broker-config reference marks log.retention.hours and log.retention.minutes “Update Mode: read-only” and log.retention.ms / log.retention.bytes “cluster-wide”. Worth knowing if you go looking: Kafka’s own prose documentation contradicts this. The “Updating Default Topic Configuration” section of Broker Configs lists log.retention.hours among the configs that “may be updated without broker restart”. The broker’s behaviour, above, agrees with the generated table and not with the prose.

Which setting wins

Ask the broker instead of guessing. --describe --all prints the whole precedence chain for a topic:

$ bin/kafka-configs.sh --bootstrap-server localhost:19092 \
    --entity-type topics --entity-name hw-expiry --describe --all
retention.ms=5000 synonyms={
    DYNAMIC_TOPIC_CONFIG:retention.ms=5000,
    DYNAMIC_DEFAULT_BROKER_CONFIG:log.retention.ms=2592000000,
    STATIC_BROKER_CONFIG:log.retention.hours=168,
    DEFAULT_CONFIG:log.retention.hours=168 }

Top of the list wins. Delete the topic override and the same command showed the topic falling back to the cluster-wide 2592000000. Set retention.ms=-1 for no time limit at all. Note the plain --describe without --all prints only the overrides — an empty result there means “inheriting”, not “unset”.

Why data you expected to expire is still there

Retention is applied to whole log segments, using the largest record timestamp in the segment, and the segment currently being written to is never deleted. A topic created with segment.ms=1000 and retention.ms=5000, with 40 messages produced in two batches:

$ ls logs/hw-expiry-0/*.log
00000000000000000000.log  00000000000000000020.log
$ bin/kafka-get-offsets.sh --bootstrap-server localhost:19092 --topic hw-expiry --time earliest
hw-expiry:0:0

...15 seconds later...

$ ls logs/hw-expiry-0/*.log
00000000000000000040.log
$ bin/kafka-get-offsets.sh --bootstrap-server localhost:19092 --topic hw-expiry --time earliest
hw-expiry:0:40

And in the broker log:

INFO [UnifiedLog partition=hw-expiry-0] Deleting segment LogSegment(baseOffset=0, size=312,
  lastModifiedTime=1786407482268, largestRecordTimestamp=1786407482155) due to log retention
  time 5000ms breach based on the largest record timestamp in the segment

That is the mechanism behind almost every “retention isn’t working” report. If your segments are the default 1 GiB and a week old (segment.bytes=1073741824, segment.ms=604800000), a low-traffic topic can hold data far past its retention.ms simply because the segment hasn’t rolled yet. Retention is also only checked every log.retention.check.interval.ms, 300000 ms — five minutes — by default.

Size limits, and what actually costs you disk

retention.bytes defaults to -1 (no size limit) and is enforced per partition, so a 10 GB cap on a 50-partition topic is a 500 GB topic. Time and size limits are both active when both are set; whichever triggers first deletes the segment.

Replication multiplies all of it. Increasing the replication factor is not a way to boost retention — it is a durability change that multiplies your disk requirement by the replication factor. Trying it with kafka-topics.sh --alter does not work either:

$ bin/kafka-topics.sh --alter --topic your_topic_name --partitions 3 --replication-factor 3 \
    --bootstrap-server localhost:19092
Option "[replication-factor]" can't be used with option "[alter]"

--replication-factor is a --create option. Changing it on an existing topic means writing a reassignment plan and running kafka-reassign-partitions.sh --reassignment-json-file plan.json --execute, as Kafka’s operations documentation shows.

Tiered storage is in Apache Kafka now

For genuinely long retention, tiered storage (KIP-405) moves completed segments to object storage and is part of Apache Kafka itself, not a vendor add-on — set remote.log.storage.system.enable=true on the brokers, plus a RemoteStorageManager implementation, then per topic:

--config remote.storage.enable=true --config local.retention.ms=86400000 \
--config retention.ms=2592000000

local.retention.ms is how long a segment stays on the broker’s disk before being offloaded; retention.ms is still the total lifetime. The documented limitations are worth reading first: no support for compacted topics, and you must disable tiered storage on every topic before disabling it at the broker level.

ZooKeeper commands no longer exist

If you are following any guide written before March 2025, check this first. Kafka 4.0 removed ZooKeeper entirely — the 4.3.1 distribution ships no ZooKeeper scripts in bin/ and no ZooKeeper jars in libs/. The compatibility notes state: “Starting with Kafka 4.0, the --zookeeper option in AdminClient commands has been removed. Users must use the --bootstrap-server option to interact with the Kafka cluster.” Confirmed on the running install:

$ bin/kafka-configs.sh --zookeeper localhost:2181 --describe --entity-type topics
zookeeper is not a recognized option

With KAFKA_HOME pointing at a Kafka 4.x distribution, how-increase-kafka-retention-period.sh starts a single-node KRaft broker that leaves any existing cluster alone, then asserts the log.retention.hours rejection, the precedence chain, the segment deletion, and the --zookeeper and --replication-factor errors.

Not tested here

This was a single-node KRaft cluster with replication factor 1 on one machine, so nothing about multi-broker rolling restarts, cross-broker rebalancing, or reassignment throttling was exercised — the kafka-reassign-partitions.sh command is quoted from Kafka’s operations documentation, not run. Tiered storage was not run either: Kafka ships no RemoteStorageManager implementation, so it needs S3/HDFS plus a plugin; those config names come from the 4.3.1 documentation and from this install’s own kafka-configs.sh --help output. Compression codecs and disk-sizing advice are left out rather than repeated unverified. Everything under a $ prompt above ran against Apache Kafka 4.3.1 on 2026-08-10.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.