# Kafka Retention

> How long a Kafka topic keeps its events before it deletes them. The default is 7 days.

**Field:** Data Engineering  
**Category:** Ingestion & Streaming  
**Also known as:** Kafka Retention Period, Topic Retention, retention.ms

## Think of it like

It is like a phone that wipes old messages after a week. It does not wait for you to hear them.

## Definition

Kafka retention is how long a topic keeps its events. The default is 7 days, and each topic can set its own. Old events are deleted when the time runs out, whether anyone read them or not. You can set a size limit instead, or keep events forever. A second mode, called compaction, keeps only the latest event for each key. Retention is what lets a new reader replay the past.

## Example

A team keeps the clicks topic for 30 days. A new report can then read a full month of clicks from the start.

## Example code

```console
$ bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter \
    --entity-type topics --entity-name clicks \
    --add-config retention.ms=2592000000
```

Keep the clicks topic for 30 days. The time is written in milliseconds.

## Why it matters

It decides how far back any reader can go. If it is too short, a broken reader loses data before it is fixed.

## Common mistake

Treating Kafka as the only copy of the data. After the retention time, the events are gone for good.

## Three principles

- **Time or size.** Delete by age or by bytes.
- **Read or not.** Old events go either way.
- **Or compact.** Keep only the latest per key.

## Related terms

- [Apache Kafka](https://learn.benibix.com/term/data/apache-kafka)
- [Kafka Topic](https://learn.benibix.com/term/data/kafka-topic)
- [Kafka Consumer](https://learn.benibix.com/term/data/kafka-consumer)
- [Kafka Offset](https://learn.benibix.com/term/data/kafka-offset)
- [Kafka Replication Factor](https://learn.benibix.com/term/data/kafka-replication-factor)

![Top: one partition as seven boxes, day 1 to day 7. Day 1 is greyed out and deleted, even if nobody read it; the newest day is highlighted. Bottom: three brokers holding the same partition. Broker 1, highlighted, is the leader and takes the writes; brokers 2 and 3 hold copies that follow it.](https://learn.benibix.com/diagrams/dat2-kafka-storage.svg)

---

Source: https://learn.benibix.com/term/data/kafka-retention
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
