Cloud

rpk topic trim-prefix

When you delete records from a topic with a timestamp, Redpanda advances the partition start offset to the first record whose timestamp is after the threshold. If record timestamps are not in order with respect to offsets, this may result in unintended deletion of data. Before using a timestamp, verify that timestamps increase in the same order as offsets in the topic to avoid accidental data loss. For example:

rpk topic consume <topic> -n 50 --format '%o %d{go[2006-01-02T15:04:05Z07:00]} %k %v'

Trim records from topics.

This command allows you to trim records from topics, to trim the topics Redpanda sets the LogStartOffset for partitions to the requested offset. All segments whose base offset is less than the requested offset are deleted, and any records within the segment before the requested offset can no longer be read.

The --offset/-o flag allows you to indicate which index you want to set the partition’s low watermark (start offset) to. It can be a single integer value denoting the offset or a timestamp if you prefix the offset with an '@'. You may select which partition you want to trim the offset from with the --partitions/-p flag.

The --from-file option allows to trim the offsets specified in a text file with the following format:

[TOPIC] [PARTITION] [OFFSET]
[TOPIC] [PARTITION] [OFFSET]
...

or the equivalent keyed JSON/YAML file.

Usage

rpk topic trim-prefix <topic> [flags]

Aliases

rpk topic trim

Examples

This section provides examples of how to use rpk topic trim-prefix.

Trim records in foo topic to offset 120 in partition 1

rpk topic trim-prefix foo --offset 120 --partitions 1

Trim records in all partitions of topic foo previous to an specific timestamp

rpk topic trim-prefix foo -o "@1622505600"

Trim records from a JSON file

rpk topic trim-prefix --from-file /tmp/to_trim.json

Flags

Value Type Description

-f, --from-file

string

Path to a file specifying topic, partition, and offset values to trim.

--no-confirm

bool

Disable confirmation prompt.

-o, --offset

string

Offset to set the partition’s start offset to (end, 47, @<timestamp>).

-p, --partitions

int32Slice

Comma-separated list of partitions to trim records from (default to all).

Global flags

Value Type Description

--config

string

Redpanda or rpk config file; default search paths are ~/.config/rpk/rpk.yaml, $PWD/redpanda.yaml, and /etc/redpanda/redpanda.yaml.

-X, --config-opt

stringArray

Override rpk configuration settings; -X help for detail or -X list for terser detail.

--ignore-profile

bool

Ignore rpk.yaml and redpanda.yaml; use default settings.

--profile

string

rpk profile to use.

-v, --verbose

bool

Enable verbose logging.