Skip to content

Archiving Event Streams

New in Marten V4 is the ability to mark an event stream and all of its events as "archived." While in the future this may have serious optimization benefits when Marten is able to utilize Postgresql sharding, today it's metadata and default filtering in the Linq querying against event data as well as asynchronous projections inside of the async daemon.

To mark a stream as archived, it's just this syntax:

public async Task SampleArchive(IDocumentSession session, string streamId)
    await session.SaveChangesAsync();

snippet source | anchor

As in all cases with an IDocumentSession, you need to call SaveChanges() to commit the unit of work.

The mt_events and mt_streams tables now both have a boolean column named is_archived.

Archived events are filtered out of all event Linq queries by default. But of course, there's a way to query for archived events with the IsArchived property of IEvent as shown below:

var events = await theSession.Events
    .Where(x => x.IsArchived)

snippet source | anchor

You can also query for all events both archived and not archived with MaybeArchived() like so:

var events = await theSession.Events.QueryAllRawEvents()
    .Where(x => x.MaybeArchived()).ToListAsync();

snippet source | anchor

Released under the MIT License.