Skip to main content

Bundle: Expiration

The expiration bundle serves a very simple purpose: it deletes the documents whose time has passed. Usage scenarios for the Expiration Bundle include storing user sessions in RavenDB or using RavenDB as a cache.

Usage

You can set the expiration date for a document using the following code:

DateTime expiry = DateTime.UtcNow.AddMinutes(5);
using (IDocumentSession session = store.OpenSession())
{
session.Store(user);
session.Advanced.GetMetadataFor(user)["Raven-Expiration-Date"] = new RavenJValue(expiry);
session.SaveChanges();
}

As you can see, all we need to do is to set the Raven-Expiration-Date property in the metadata for the appropriate date, and, at the specified time, the document will be automatically deleted.

The date must be UTC, not local time.

When master-master replication is set between servers, the Expiration bundle should be turned on ONLY on one server, otherwise conflicts will occur.