---
title: "Schedule Document Archiving"
description: "Schedule single or multiple RavenDB documents for archiving at a specific future time using the Client API or Studio interface."
url: "https://docs.ravendb.net/7.2/data-archival/schedule-document-archiving"
version: "7.2"
language: "csharp"
---

# Schedule Document Archiving

> 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**
>
> - Documents cannot be archived directly - they must be scheduled for archival.
>   To **schedule a document** for archival, add the `@archive-at` property to the document's metadata and set its value to the desired archival time (in UTC).  
>   This can be done in several ways, as described in this article.
>
> - **Note**:  
>   Just scheduling a document for archival does Not archive it at the specified time.  
>   Actual archiving is performed only by a background task that runs when the archival feature is [enabled](https://docs.ravendb.net/7.2/data-archival/enable-data-archiving.md).  
>   This task periodically scans the database for documents scheduled for archival.  
>   The scan frequency is configurable when [enabling ](https://docs.ravendb.net/7.2/data-archival/enable-data-archiving.md) the archival feature (default: 60 seconds).
>
> - The archiving task will archive any document whose `@archive-at` time has passed at the time of the scan.  
>   The `@archive-at` metadata property will then be replaced with `@archived: true`.
>
> - You can schedule documents for archival even if the archiving feature is not yet enabled.
>   These documents will be archived once the feature is enabled and the task runs - provided the scheduled time has already passed.
>
> - In this article:
>   - [Schedule a SINGLE document for archiving - from the Client API](#schedule-a-single-document-for-archiving-from-the-client-api)
>   - [Schedule a SINGLE document for archiving - from the Studio](#schedule-a-single-document-for-archiving-from-the-studio)
>   - [Schedule MULTIPLE documents for archiving - from the Client API](#schedule-multiple-documents-for-archiving-from-the-client-api)
>   - [Schedule MULTIPLE documents for archiving - from the Studio](#schedule-multiple-documents-for-archiving-from-the-studio)

## Schedule a SINGLE document for archiving - from the Client API {#schedule-a-single-document-for-archiving-from-the-client-api}

To schedule a single document for archival from the Client API,  
add the `@archive-at` property directly to the document metadata as follows:

### Schedule\_document

```csharp
using (var session = store.OpenSession())
{
    // Load the document to schedule for archiving
    var company = session.Load<Company>("companies/91-A");
    
    // Access the document's metadata
    var metadata = session.Advanced.GetMetadataFor(company);
    
    // Set the future archival date (in UTC)
    var archiveDate = SystemTime.UtcNow.AddDays(1);
    metadata["@archive-at"] = archiveDate;
    
    // Save the changes
    session.SaveChanges();
}
```

### Schedule\_document\_async

```csharp
using (var asyncSession = store.OpenAsyncSession())
{
    // Load the document to schedule for archiving
    var company = await asyncSession.LoadAsync<Company>("companies/91-A");
    
    // Access the document's metadata
    var metadata = asyncSession.Advanced.GetMetadataFor(company);

    // Set the future archival date (in UTC)
    var archiveDate = SystemTime.UtcNow.AddDays(1);
    metadata["@archive-at"] = archiveDate;
    
    // Save the changes
    await asyncSession.SaveChangesAsync();
}
```

Learn more about modifying the metadata of a document in [Modifying Document Metadata](https://docs.ravendb.net/7.2/client-api/session/how-to/get-and-modify-entity-metadata.md).

## Schedule a SINGLE document for archiving - from the Studio {#schedule-a-single-document-for-archiving-from-the-studio}

- To schedule a single document for archival from the Studio:
  - Open the Document view.
  - Add the `@archive-at` property to the document's metadata.
  - Set its value to the desired archive time in UTC format.
  - Save the document.

![Schedule a document for archiving](https://docs.ravendb.net/assets/images/schedule-document-for-archiving-132af2b2fc447e54380c42b802787f6b.png)

1. This is the `@archive-at` property that was added to the document's metadata.  
   E.g.: `"@archive-at": "2025-06-25T14:00:00.0000000Z"`
2. After saving the document, the Studio displays the time remaining until the document is archived.

## Schedule MULTIPLE documents for archiving - from the Client API {#schedule-multiple-documents-for-archiving-from-the-client-api}

- Use the `PatchByQueryOperation` to schedule multiple documents for archiving.

- In the **patch query**, you can apply any filtering condition to select only the documents you want to archive.  
  In the **patch script**, call the `archived.archiveAt` method to set the desired archival time (in UTC).

- When the patch operation is executed,  
  the server will add the `@archive-at` property to the metadata of all documents that match the query filter.

**Example:**

The following example schedules all orders that were made at least a year ago for archival.  
The **patch query** filters for these older orders.  
Any document matching the query is then scheduled for archival by the **patch script**.

### Schedule\_documents

```csharp
var archiveDate = SystemTime.UtcNow.AddDays(1);
string archiveDateString = archiveDate.ToString("o");

var oldDate = SystemTime.UtcNow.AddYears(-1);
string oldDateString = oldDate.ToString("o");

// Define the patch query string
// Request to archive all Orders older than one year
string patchByQuery = $@"
    // The patch query:
    // ================
    from Orders
    where OrderedAt < '{oldDateString}'
    update {{
        // The patch script - schedule for archival:
        // =========================================
        archived.archiveAt(this, '{archiveDateString}')
    }}";

// Define the patch operation, pass the patch query string
var patchByQueryOp = new PatchByQueryOperation(patchByQuery);
    
// Execute the operation by passing it to Operations.Send
store.Operations.Send(patchByQueryOp);
```

### Schedule\_documents\_async

```csharp
var archiveDate = SystemTime.UtcNow.AddDays(1);
string archiveDateString = archiveDate.ToString("o");

var oldDate = SystemTime.UtcNow.AddYears(-1);
string oldDateString = oldDate.ToString("o");

// Define the patch query string
// Request to archive all Orders older than one year
string patchByQuery = $@"
    from Orders
    where OrderedAt < '{oldDateString}'
    update {{
        archived.archiveAt(this, '{archiveDateString}')
    }}";

// Define the patch operation, pass the patch query string
var patchByQueryOp = new PatchByQueryOperation(patchByQuery);
    
// Execute the operation by passing it to Operations.SendAsync
await store.Operations.SendAsync(patchByQueryOp);
```

### Operation\_overload

```csharp
var archiveDate = SystemTime.UtcNow.AddDays(1);
string archiveDateString = archiveDate.ToString("o");

var oldDate = SystemTime.UtcNow.AddYears(-1);
string oldDateString = oldDate.ToString("o");

// Define the patch string
// Request to archive all Orders older than one year
string patchByQuery = $@"
    from Orders
    where OrderedAt < $p0 
    update {{
        archived.archiveAt(this, $p1)
    }}";

// Define the patch operation, pass the patch query
var patchByQueryOp = new PatchByQueryOperation(new IndexQuery()
{
    Query = patchByQuery,
    QueryParameters = new Parameters()
    {
        { "p0", oldDateString },
        { "p1", archiveDateString }
    }
});
    
// Execute the operation by passing it to Operations.Send
store.Operations.Send(patchByQueryOp);
```

### Operation\_overload\_async

```csharp
var archiveDate = SystemTime.UtcNow.AddDays(1);
string archiveDateString = archiveDate.ToString("o");

var oldDate = SystemTime.UtcNow.AddYears(-1);
string oldDateString = oldDate.ToString("o");

// Define the patch string
// Request to archive all Orders older than one year
string patchByQuery = $@"
    from Orders
    where OrderedAt < $p0 
    update {{
        archived.archiveAt(this, $p1)
    }}";

// Define the patch operation, pass the patch query
var patchByQueryOp = new PatchByQueryOperation(new IndexQuery()
{
    Query = patchByQuery,
    QueryParameters = new Parameters()
    {
        { "p0", oldDateString },
        { "p1", archiveDateString }
    }
});
    
// Execute the operation by passing it to Operations.SendAsync
await store.Operations.SendAsync(patchByQueryOp);
```

**Syntax:**

```csharp
archived.archiveAt(doc, utcDateTimeString)
```

| Parameter | Type | Description |
| - | - | - |
| **doc** | `object` | The document to schedule for archiving. |
| **utcDateTimeString** | `string` | The UTC timestamp (as a string) that specifies when the document should be archived. |

To learn more about the `PatchByQueryOperation`, see [Set-based patch operations](https://docs.ravendb.net/7.2/documents/patching-documents/patch-multiple-documents/client-api.md).

## Schedule MULTIPLE documents for archiving - from the Studio {#schedule-multiple-documents-for-archiving-from-the-studio}

- To schedule multiple documents for archiving from the Studio:
  - Open the Patch view.
  - Enter a patch script that uses the `archived.archiveAt` method, providing the desired archive date in UTC.
  - Execute the patch.

**Example**:

The following patch script, used directly in the Studio,  
performs the same operation as the [Client API example](#schedule-multiple-documents-for-archiving-from-the-client-api) shown above.

![Schedule multiple documents for archiving](https://docs.ravendb.net/assets/images/schedule-multiple-documents-for-archiving-cad03d75d590a1d7cdd4bdf245755fb5.png)

1. Open the Patch view.
2. Enter the patch script.
3. Click to execute the patch.
4. You can test the patch on a sample document before executing the whole operation.  
   Learn more in [Test patch](https://docs.ravendb.net/7.2/documents/patching-documents/patch-multiple-documents/patch-view.md#test-patch).

## See also

- [Overview](https://docs.ravendb.net/7.2/data-archival/overview.md)
- [Enable data archiving](https://docs.ravendb.net/7.2/data-archival/enable-data-archiving.md)
- [Archived documents and other features](https://docs.ravendb.net/7.2/data-archival/archived-documents-and-other-features.md)
- [Unarchiving documents](https://docs.ravendb.net/7.2/data-archival/unarchiving-documents.md)
- [Overview](https://docs.ravendb.net/7.2/server/configuration/configuration-options.md#settings.json)
- [Database Settings](https://docs.ravendb.net/7.2/studio/database/settings/database-settings.md#view-database-settings)
- [Document Expiration](https://docs.ravendb.net/7.2/documents/document-expiration/client-api.md)
- [Patch By Query](https://docs.ravendb.net/7.2/client-api/rest-api/queries/patch-by-query.md)
