---
title: "Compare Exchange Expiration"
url: "https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/compare-exchange-expiration"
version: "6.2"
language: "csharp"
---

# Compare Exchange Expiration

> Client language: C#.
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Compare exchange value expiration works very similar to [document expiration](https://docs.ravendb.net/6.2/server/extensions/expiration.md).
>
> - Use the `@expires` field in a [compare exchange value's metadata](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/compare-exchange-metadata.md) to schedule its expiration.
>
> - In this page:
>   - [Syntax](#syntax)
>   - [Examples](#examples)

## Syntax

`@expires` is a metadata property that holds a `DateTime` value. Once the
specified date and time has passed, the compare exchange value is set for
deletion by the expiration feature. The *exact* time this happens depends
on the expiration frequency and other
[expiration configurations](https://docs.ravendb.net/6.2/server/extensions/expiration.md#configuring-the-expiration-feature).

To set a compare exchange value to expire, simply put a `DateTime` value
(in UTC format) in the `@expires` field, then to send it to the server.

## Examples

Creating a new key with `CreateCompareExchangeValue()`:

```csharp
using (IAsyncDocumentSession session = store.OpenAsyncSession())
{
  // Set a time exactly one week from now
  DateTime expirationTime = DateTime.UtcNow.AddDays(7);

  // Create a new compare exchange value
  var cmpxchgValue = session.Advanced.ClusterTransaction.CreateCompareExchangeValue("key", "value");

  // Edit Metadata
  cmpxchgValue.Metadata[Constants.Documents.Metadata.Expires] = expirationTime;

  // Send to server
  session.SaveChangesAsync();
}
```

Updating an existing key with `PutCompareExchangeValueOperation<T>`:

```csharp
// Retrieve an existing key
CompareExchangeValue<string> cmpxchgValue = store.Operations.Send(
      new GetCompareExchangeValueOperation<string>("key"));
              
// Set time
DateTime expirationTime = DateTime.UtcNow.AddDays(7);

// Edit Metadata
cmpxchgValue.Metadata[Constants.Documents.Metadata.Expires] = expirationTime;
              
// Update value. Index must match the index on the server side,
// or the operation will fail.
CompareExchangeResult<string> result = store.Operations.Send(
      new PutCompareExchangeValueOperation<string>(
          cmpxchgValue.Key,
          cmpxchgValue.Value,
          cmpxchgValue.Index));
```

## See also

- [Session: How to Get and Modify Entity Metadata](https://docs.ravendb.net/6.2/client-api/session/how-to/get-and-modify-entity-metadata.md)
- [Compare Exchange: Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.md)
- [Compare Exchange Metadata](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/compare-exchange-metadata.md)
- [Document Expiration](https://docs.ravendb.net/6.2/server/extensions/expiration.md)
