Trim and delete

Trim and delete

Remove old records from a stream with a trim, and remove streams and buckets you no longer need.

A trim removes the records before a point and keeps the stream. Deleting a stream removes it with all its records, and deleting a bucket removes a bucket that holds no streams. To trim on a schedule instead, give the stream a retention. Trimming and Retention explain the ideas. The API reference has trim and the stream and bucket deletes. The CLI has nova stream trim, nova stream delete, and nova bucket delete.

Trim a stream

Trim to the bookmark of the records you have processed. The records before it are unreadable at once, and the auditor, the background process that reclaims storage, frees their storage later:

page, err := c.Read(ctx, stream, client.FromBeginning(), 100)
if err != nil {
	return err
}

// ... process page.Records ...

// Everything before the bookmark is processed: let it go.
if err := c.Trim(ctx, stream, client.ToBookmark(page.Bookmark)); err != nil {
	return err
}

fmt.Println("trimmed everything before", page.Bookmark)
Output
trimmed everything before 2
ToBookmark(b)Bookmark
Everything before a place a read or subscription returned
ToSeq(n)Sequence number
Everything before this sequence number. Refused on a scalable stream
ToTime(ms)Time
Every record stamped before this time, in milliseconds

A trim only moves forward. A trim to an earlier point does nothing, so running one again is safe. Sequence numbers stay as they were, and the next record still gets the next number. A read that starts below the trim point fails with ErrTrimmed / TrimmedError (HTTP 410); read with the clamp option to start at the first record left. A trim needs a stream that exists.

To trim on a schedule, set the stream's retention, a maximum age or size, when you create it or change its settings. Nova then trims the records past it on its own.

Delete a stream

DeleteStream / deleteStream stops the stream's writers, then removes the stream:

stream := client.Address{Bucket: "demo", Key: "events"}

if err := c.DeleteStream(ctx, stream); err != nil {
	return err
}

fmt.Println("deleted", stream)
Output
deleted demo/events

Once the call returns, every append, read, and subscription of the stream fails with ErrStreamNotFound / StreamNotFoundError (HTTP 404). Deleting it again succeeds, and deleting a stream that never existed fails with not found. The address is free at once: a stream you create there is a new stream, with a new ID and no records. The auditor reclaims the old stream's storage in the background.

Delete a bucket

DeleteBucket / deleteBucket removes a bucket that holds no streams:

if err := c.DeleteBucket(ctx, "demo"); err != nil {
	return err
}

fmt.Println("deleted the bucket demo")
Output
deleted the bucket demo

A deleted stream still counts as being in its bucket until the auditor has reclaimed its storage, which takes a few minutes. So a bucket delete right after its last stream's delete is refused, as it is while a stream lives in the bucket. The Go SDK returns a FailedPrecondition gRPC status, and the TypeScript SDK a TransportError with code FAILED_PRECONDITION. Try again a few minutes later.

The HTTP refusal says to retry in a second

Over HTTP the refusal is a 503 with Retry-After: 1. A client that follows the header retries for minutes, and without end while a stream lives in the bucket. Wait minutes between tries, and stop after a few.

See also

On this page