Skip to main content

Compare-Exchange Expiration

  • Compare-exchange items can be set to be deleted automatically in a future time.

  • To schedule expiration for a compare-exchange item, set the @expires field in the item's metadata.
    This can be done when creating a new item or updating an existing one.

  • RavenDB scans the database periodically to remove expired items. Any compare-exchange item whose @expires timestamp has passed at the time of the scan will be automatically removed.

  • The scan frequency is configurable -
    see the Cluster.CompareExchangeExpiredDeleteFrequencyInSec configuration key. The default is 60 seconds.

  • To manually remove a compare-exchange item, see Delete compare-exchange items.

  • Note: The compare-exchange expiration feature is not related to document expiration.
    You do NOT need to enable document expiration in order to use compare-exchange expiration.



Add expiration date using the Client API

// The session must be opened in cluster-wide mode
using (var session = store.OpenSession(
new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
// Call 'CreateCompareExchangeValue', specify the item's KEY and VALUE
CompareExchangeValue<string> item =
session.Advanced.ClusterTransaction.CreateCompareExchangeValue(
key: "user1-name@example.com",
value: "users/1"
);

// Add METADATA fields to the item
// Set a future UTC DateTime in the `@expires` field to schedule expiration
// "Constants.Documents.Metadata.Expires" = "@expires"
item.Metadata[Constants.Documents.Metadata.Expires] = DateTime.UtcNow.AddDays(7);

// The item will be created on the server once 'SaveChanges' is called
session.SaveChanges();
}

Add expiration date using the Studio

  • You can set or update the expiration date of a compare-exchange item directly from the Studio.

  • Go to Documents > Compare Exchange.
    Edit an existing item or create a new one.
    In the item's metadata, set the @expires field to a future UTC date/time (ISO 8601 format).

The compare-exchange view


Syntax