Skip to content

Data Retention

You can configure a Backup to delete old data periodically. Currently, we support these retention policies:

  • Keep (the default): all data is kept forever;
  • Delete: the data is deleted after some duration you provide.

Policies are defined on a backup using a set of labels, the main one being io.kannika/data-retention-policy to select the policy. See details for each policy below.

When using this policy, your data won’t be deleted at any point. This is the default policy if none is defined, but if you wish to set it explicitly, then set the io.kannika/data-retention-policy to Keep.

Nothing is ever deleted from the storage under this policy. The segment reaper does not run for the Backup, so segments that expired under an earlier Delete policy stay in the storage, and so do orphaned segment files: files that are not part of the backup.

apiVersion: kannika.io/v1alpha
kind: Backup
metadata:
name: backup-example
labels:
io.kannika/data-retention-policy: Keep
spec:
source: "my-eventhub"
sink: "my-storage"
streams:
- topic: "active-stream"

The Delete policy will mark segments as expired after some duration of your choosing. The expiration date of a segment is computed using the latest record timestamp it contains.

Once a segment’s expiration date has passed, it becomes eligible for deletion. See How expired data is removed for when that actually happens, and Changing the retention policy on an existing Backup for adjusting the duration later.

The duration is expected to match this regular expression: ([0-9]+(ns|us|ms|[smhdwy]))+.

apiVersion: kannika.io/v1alpha
kind: Backup
metadata:
name: backup-example
labels:
# Mark segments as expired after 30 days, and remove them.
io.kannika/data-retention-policy: Delete
io.kannika/data-retention-policy-delete-after: 30d
spec:
source: "my-eventhub"
sink: "my-storage"
streams:
- topic: "active-stream"

Avoid the Delete policy on compacted topics (see Compacted topics).

Most Kafka clusters hold three kinds of topics: topics that Kafka keeps forever, topics that Kafka drops data from after a while, and compacted topics. Each kind needs a different retention, and a retention policy currently applies to a whole Backup, so back them up in separate Backups, one per kind. This may change in a future release. A topic selector on each Backup picks its topics from the topic configuration, so new topics land in the right Backup without changing it.

Use the Keep policy to keep all data forever. This suits archives and compliance, when the cost of a growing storage is acceptable.

apiVersion: kannika.io/v1alpha
kind: Backup
metadata:
name: archive-backup
labels:
io.kannika/data-retention-policy: Keep
spec:
source: "my-eventhub"
sink: "archive-storage"
# Topics that Kafka keeps forever (retention.ms -1), except compacted and repartition topics
topicSelectors:
matchers:
- allOf:
- property:
key: "cleanup.policy"
operator: NotContains
value: "compact"
- property:
key: "retention.ms"
operator: Equals
value: "-1"
excludeMatchers:
- name:
glob: "*-repartition"

Use the Delete policy with a duration to remove data older than that duration. This suits regular, non-compacted topics where only recent data matters.

apiVersion: kannika.io/v1alpha
kind: Backup
metadata:
name: recent-backup
labels:
io.kannika/data-retention-policy: Delete
io.kannika/data-retention-policy-delete-after: 30d
spec:
source: "my-eventhub"
sink: "recent-storage"
# Topics that Kafka drops data from after a while, except compacted topics
topicSelectors:
matchers:
- allOf:
- property:
key: "cleanup.policy"
operator: NotContains
value: "compact"
- property:
key: "retention.ms"
operator: NotEquals
value: "-1"

Back up the compacted topics in a separate Backup with the Keep policy, and rotate its storage on a schedule, as described in Rotate backup storage on a schedule. The Delete policy would remove keys that still hold their latest value (see Compacted topics).

apiVersion: kannika.io/v1alpha
kind: Backup
metadata:
name: compacted-backup
labels:
io.kannika/data-retention-policy: Keep
spec:
source: "my-eventhub"
sink: "compacted-storage"
# Compacted topics
topicSelectors:
matchers:
- property:
key: "cleanup.policy"
operator: Contains
value: "compact"

While a Backup with the Delete policy is running, a background task (the segment reaper) periodically checks each topic for expired segments and deletes them from the storage. If the Backup job is stopped, expired files are not removed until it runs again, though they will be ignored by an eventual Restore in the meantime.

Deletion is not immediate. After a segment expires, the reaper leaves it untouched for a grace period (one hour by default, see Configuring the segment reaper) so it is never removed while a Restore might still be reading it. It then schedules its next check from the earliest upcoming expiration, checking at most once an hour when nothing is close to expiring and more frequently as an expiration approaches.

The reaper also cleans up orphaned segment files once they are older than the grace period.

The Backup logs each segment file it deletes at the info level. See Logging for how to adjust the log level and format.

Two annotations on the Backup tune the reaper of a Backup with the Delete policy:

  • io.kannika/segment-reaper-grace-period: how long the reaper leaves a segment file untouched before it deletes it. The default is one hour. It takes a duration such as 30s or 2h, and it must be above zero. A short grace period can remove a file from under a Restore that started before the segment expired and is still reading it.
  • io.kannika/segment-reaper-batch-size: the most segments one check of a topic takes. Each segment costs a request to the storage, so a check stays short however many segments are waiting for it. The default is 10,000. It takes a whole number above zero.

Annotation values are strings, so quote the batch size. The reaper annotations are annotations only, unlike the retention policy labels above.

apiVersion: kannika.io/v1alpha
kind: Backup
metadata:
name: backup-example
labels:
io.kannika/data-retention-policy: Delete
io.kannika/data-retention-policy-delete-after: 30d
annotations:
io.kannika/segment-reaper-grace-period: 30m
io.kannika/segment-reaper-batch-size: "250"
spec:
source: "my-eventhub"
sink: "my-storage"
streams:
- topic: "active-stream"

An invalid value, such as a grace period of 0s or a batch size of 0, fails the Backup’s deployment. The operator reports the error Invalid value '<value>' for annotation '<key>'. Without the Delete policy, the annotations are ignored.

Changing the retention policy on an existing Backup

Section titled “Changing the retention policy on an existing Backup”

You can set or change the retention policy at any time, either from the Console or by editing the labels on the Backup resource directly. This works the same whether the Backup currently uses the Keep policy or an existing Delete policy.

Segments that have not expired yet get their expiration date recalculated against the new duration. Shortening the duration therefore pulls more segments into scope for deletion. The reaper removes them the next time it runs, so the Backup job needs to be running for the expired files to actually be deleted.

Switching from Delete to Keep, or removing the policy, stops all deletion. Segments that already expired under the Delete policy stay in the storage.

Avoid the Delete policy on compacted topics. Segments expire by age, not by key, so a key that has not been updated within the configured duration is removed from the backup, even though the topic still holds its latest value. A Restore from that backup no longer contains those keys.

To keep the storage of compacted topics from growing indefinitely, back them up in a separate Backup, as described in Back up compacted topics, and rotate its storage, as described in Rotating backup storage.

Rotating backup storage means pointing a Backup at a new Storage at regular intervals, for example every day or every month. The Backup continues in the new Storage, and the previous Storage stays as it was, so you can still restore from it.

Use rotation when you need a separate archive per period, for example for compliance and audits, or to keep the storage of compacted topics from growing indefinitely.

To rotate, pause the Backup, create a new Storage, set it as the sink of the Backup, and resume the Backup. See Rotate backup storage on a schedule to automate this with a Kubernetes CronJob.

A retention policy is the right tool for deleting old data. If instead you want to remove a topic’s backup entirely as a one-off, delete that topic’s directory directly from the storage. See Deleting a topic’s data for the procedure.