Skip to content
On this page

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:

cs
public async Task SampleArchive(IDocumentSession session, string streamId)
{
    session.Events.ArchiveStream(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:

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

snippet source | anchor

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

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

snippet source | anchor

Released under the MIT License.