---
title: "Unarchiving Documents"
url: "https://docs.ravendb.net/6.2/data-archival/unarchiving-documents"
version: "6.2"
language: "csharp"
---

# Unarchiving Documents

> 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**
>
> - Archived documents can be unarchived at any time.
>
> - The archiving feature does Not need to be enabled to unarchive documents.  
>   Any previously archived document can be unarchived, regardless of the feature's current state.
>
> - Do **not** attempt to unarchive a document by manually removing the `@archived: true` metadata property from the document.
>   This will not clear the internal archived status of the document.  
>   To properly unarchive a document, use the `archived.unarchive()` method as described below.
>
> - In this article:
>   - [Unarchive documents - from the Client API](#unarchive-documents---from-the-client-api)
>   - [Unarchive documents - from the Studio](#unarchive-documents---from-the-studio)
>   - [Unarchiving documents with index-based patch queries](#unarchiving-documents-with-index-based-patch-queries)

## Unarchive documents - from the Client API

- To unarchive documents from the Client API, use the `PatchByQueryOperation` operation,  
  which targets one or more documents based on the patch query.

- You can apply any filtering condition within the query to target only the documents you want to unarchive.

- Within the **patch script**, call the `archived.unarchive()` method to unarchive all documents  
  that match the **patch query**.
  **Example:**

The following operation will unarchive ALL archived documents in the *Orders* collection.

### Unarchive\_documents

```csharp
// Define the patch query string
string patchByQuery = @"
  // The patch query:
  // ================
  from Orders
  update 
  {
      // The patch script:
      // =================
      archived.unarchive(this)
  }";

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

### Unarchive\_documents\_async

```csharp
// Define the patch query string
string patchByQuery = @"
  from Orders
  update 
  {
      archived.unarchive(this)
  }";

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

### Operation\_overload

```csharp
// Define the patch query string
string patchByQuery = @"
  from Orders
  update 
  {
      archived.unarchive(this)
  }";

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

### Operation\_overload\_async

```csharp
// Define the patch query string
string patchByQuery = @"
  from Orders
  update 
  {
      archived.unarchive(this)
  }";

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

**Syntax:**

```csharp
archived.unarchive(doc)
```

| Parameter | Type | Description |
| - | - | - |
| **doc** | `object` | The document to unarchive. |

## Unarchive documents - from the Studio

- To unarchive documents from the Studio:
  - Open the Patch view.
  - Enter a patch script that uses the `archived.unarchive()` method.
  - Execute the patch.
    **Example**:

The following patch script, used directly in the Studio,
performs the same operation as the [Client API example](#unarchive-documents---from-the-client-api) shown above.
It will unarchive all archived documents in the *Orders* collection.

![Unarchive documents](https://docs.ravendb.net/assets/images/unarchive-documents-68c8a10de5fe373e1c976ddfaeae548d.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/6.2/studio/database/documents/patch-view.md#test-patch).

## Unarchiving documents with index-based patch queries

- When running a patch query to unarchive documents over an index,  
  you need to consider the index configuration regarding archived documents.

- If the index is configured to exclude archived documents, the query portion of the patch operation will not match any archived documents -
  because those documents are not included in the index.  
  As a result, no documents will be unarchived by the patch operation.

- For example, the following patch query uses a dynamic query that creates an auto-index.  
  If the [Indexing.Auto.ArchivedDataProcessingBehavior](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md#indexingautoarchiveddataprocessingbehavior) configuration key is set to its default `ExcludeArchived` value,
  then even if archived documents exist in the *Orders* collection with `ShipTo.Country == 'USA'`, they will not be matched -  because the auto-index does not include them -  
  and the patch operation will not unarchive any documents.

```csharp
string patchByQuery = @"
  // This filtering query creates an auto-index:
  // ===========================================
  from Orders
  where ShipTo.Country == 'USA'
  update 
  {
      archived.unarchive(this)
  }";

var patchByQueryOp = new PatchByQueryOperation(patchByQuery);
store.Operations.Send(patchByQueryOp);
```

Two possible workarounds are:

1. **Configure the index to include archived documents**:  
   This ensures archived documents are included in the index and can be matched by the patch query.  
   Use this option only if including archived documents in the index aligns with your indexing strategy.

   **For auto-indexes**:  
   Set the [Indexing.Auto.ArchivedDataProcessingBehavior](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md#indexingautoarchiveddataprocessingbehavior) configuration key to `IncludeArchived`.  
   **For static-indexes**:  
   Set the [Indexing.Static.ArchivedDataProcessingBehavior](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md#indexingstaticarchiveddataprocessingbehavior) configuration key to `IncludeArchived`,  
   or - configure the definition of the specific static-index to include archived documents.  
   See [Archived documents and indexing](https://docs.ravendb.net/6.2/data-archival/archived-documents-and-other-features.md#archived-documents-and-indexing).

2. **Use a collection query instead of an index query**:  
   Run a simple collection-based query that does not rely on an index.  
   Apply your filtering logic inside the patch script to unarchive only the relevant documents.

   For example:

```csharp
string patchByQuery = @"
  // Perform a collection query:
  // ===========================
  from Orders as order
  update 
  { 
      // Filter documents inside the script:
      // ===================================
      if (order.ShipTo.City == 'New York')
      {
          archived.unarchive(this)
      }
  }";

var patchByQueryOp = new PatchByQueryOperation(patchByQuery);
store.Operations.Send(patchByQueryOp);
```

## See also

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