---
title: "Compact Database Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/server-wide/compact-database"
version: "6.2"
language: "csharp"
---

# Compact Database Operation

> Client language: C#. Also available for [Python](https://docs.ravendb.net/6.2/client-api/operations/server-wide/compact-database.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/operations/server-wide/compact-database.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/server-wide/compact-database.nodejs.md).
>
> 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**
>
> - Use the `CompactDatabaseOperation` compaction operation to **removes empty gaps on disk**
>   that still occupy space after deletes.  
>   You can choose whether to compact *documents* and/or *selected indexes*.
>
> - **During compaction the database will be offline**.  
>   The operation is a executed asynchronously as a background operation and can be awaited.
>
> - The operation will **compact the database on one node**.  
>   To compact all database-group nodes, the command must be sent to each node separately.
>
> - **Target node**:  
>   By default, the operation will be executed on the server node that is defined by the
>   [client configuration](https://docs.ravendb.net/6.2/client-api/configuration/load-balance/overview.md#client-logic-for-choosing-a-node).  
>   The operation can be executed on a specific node by using the
>   [ForNode](https://docs.ravendb.net/6.2/client-api/operations/how-to/switch-operations-to-a-different-node.md) method.
>
> - **Target database**:  
>   The database to compact is specified in `CompactSettings` (see examples below).  
>   An exception is thrown if the specified database doesn't exist on the server node.
>
> - In this page:
>   - [Examples](#examples):
>     - [Compact documents](#examples)
>     - [Compact specific indexes](#compact-specific-indexes)
>     - [Compact all indexes](#compact-all-indexes)
>     - [Compact on other nodes](#compact-on-other-nodes)
>   - [Compaction triggers compression](#compaction-triggers-compression)
>   - [Compact from Studio](#compact-from-studio)
>   - [Syntax](#syntax)

## Examples

#### Compact documents:

The following example will compact only **documents** for the specified database.

##### Sync

```csharp
// Define the compact settings
CompactSettings settings = new CompactSettings
{
  // Database to compact
  DatabaseName = "Northwind",

  // Set 'Documents' to true to compact all documents in database
  // Indexes are not set and will not be compacted
  Documents = true
};

// Define the compact operation, pass the settings
IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);

// Execute compaction by passing the operation to Maintenance.Server.Send
Operation operation = documentStore.Maintenance.Server.Send(compactOp);

// Wait for operation to complete, during compaction the database is offline
operation.WaitForCompletion();
```

##### Async

```csharp
// Define the compact settings
CompactSettings settings = new CompactSettings
{
  // Database to compact
  DatabaseName = "Northwind",

  // Set 'Documents' to true to compact all documents in database
  // Indexes are not set and will not be compacted
  Documents = true
};

// Define the compact operation, pass the settings
IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);

// Execute compaction by passing the operation to Maintenance.Server.SendAsync
Operation operation = await documentStore.Maintenance.Server.SendAsync(compactOp);

// Wait for operation to complete, during compaction the database is offline
await operation.WaitForCompletionAsync().ConfigureAwait(false);
```

#### Compact specific indexes:

The following example will compact only specific indexes.

##### Sync

```csharp
// Define the compact settings
CompactSettings settings = new CompactSettings
{
  // Database to compact
  DatabaseName = "Northwind",
  
  // Setting 'Documents' to false will compact only the specified indexes
  Documents = false,
  
  // Specify which indexes to compact
  Indexes = new[] { "Orders/Totals", "Orders/ByCompany" }, 
  
  // Optimize indexes is Lucene's feature to gain disk space and efficiency
  // Set whether to skip this optimization when compacting the indexes
  SkipOptimizeIndexes = false
};

// Define the compact operation, pass the settings
IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);

// Execute compaction by passing the operation to Maintenance.Server.Send
Operation operation = documentStore.Maintenance.Server.Send(compactOp);
// Wait for operation to complete
operation.WaitForCompletion();
```

##### Async

```csharp
// Define the compact settings
CompactSettings settings = new CompactSettings
{
  // Database to compact
  DatabaseName = "Northwind",
  
  // Setting 'Documents' to false will compact only the specified indexes
  Documents = false,
  
  // Specify which indexes to compact
  Indexes = new[] { "Orders/Totals", "Orders/ByCompany" }, 
  
  // Optimize indexes is Lucene's feature to gain disk space and efficiency
  // Set whether to skip this optimization when compacting the indexes
  SkipOptimizeIndexes = false
};

// Define the compact operation, pass the settings
IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);

// Execute compaction by passing the operation to Maintenance.Server.SendAsync
Operation operation = await documentStore.Maintenance.Server.SendAsync(compactOp);
// Wait for operation to complete
await operation.WaitForCompletionAsync().ConfigureAwait(false);
```

#### Compact all indexes:

The following example will compact all indexes and documents.

##### Sync

```csharp
// Get all indexes names in the database using the 'GetIndexNamesOperation' operation
// Use 'ForDatabase' if the target database is different than the default database defined on the store
string[] allIndexNames =
  documentStore.Maintenance.ForDatabase("Northwind")
      .Send(new GetIndexNamesOperation(0, int.MaxValue));

// Define the compact settings
CompactSettings settings = new CompactSettings
{
  DatabaseName = "Northwind", // Database to compact
  
  Documents = true,           // Compact all documents

  Indexes = allIndexNames,    // All indexes will be compacted
  
  SkipOptimizeIndexes = true  // Skip Lucene's indexes optimization
};

// Define the compact operation, pass the settings
IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);

// Execute compaction by passing the operation to Maintenance.Server.Send
Operation operation = documentStore.Maintenance.Server.Send(compactOp);
// Wait for operation to complete
operation.WaitForCompletion();
```

##### Async

```csharp
// Get all indexes names in the database using the 'GetIndexNamesOperation' operation
// Use 'ForDatabase' if the target database is different than the default database defined on the store
string[] allIndexNames =
  documentStore.Maintenance.ForDatabase("Northwind")
      .Send(new GetIndexNamesOperation(0, int.MaxValue));

// Define the compact settings
CompactSettings settings = new CompactSettings
{
  DatabaseName = "Northwind", // Database to compact
  
  Documents = true,           // Compact all documents

  Indexes = allIndexNames,    // All indexes will be compacted
  
  SkipOptimizeIndexes = true  // Skip Lucene's indexes optimization
};

// Define the compact operation, pass the settings
IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);

// Execute compaction by passing the operation to Maintenance.Server.SendAsync
Operation operation = await documentStore.Maintenance.Server.SendAsync(compactOp);
// Wait for operation to complete
await operation.WaitForCompletionAsync();
```

#### Compact on other nodes:

- By default, an operation executes on the server node that is defined by the [client configuration](https://docs.ravendb.net/6.2/client-api/configuration/load-balance/overview.md#client-logic-for-choosing-a-node).
- The following example will compact the database on all [member](https://docs.ravendb.net/6.2/server/clustering/rachis/cluster-topology.md#nodes-states-and-types) nodes from its database-group topology.  
  `ForNode` is used to execute the operation on a specific node.

##### Sync

```csharp
// Get all member nodes in the database-group using the 'GetDatabaseRecordOperation' operation
List<string> allMemberNodes =
  documentStore.Maintenance.Server.Send(new GetDatabaseRecordOperation("Northwind"))
      .Topology.Members;

// Define the compact settings as needed
CompactSettings settings = new CompactSettings
{
  // Database to compact
  DatabaseName = "Northwind",
  
  //Compact all documents in database
  Documents = true
};

// Execute the compact operation on each member node
foreach (string nodeTag in allMemberNodes)
{
  // Define the compact operation, pass the settings
  IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);
  
  // Execute the operation on a specific node
  // Use `ForNode` to specify the node to operate on
  Operation operation = documentStore.Maintenance.Server.ForNode(nodeTag).Send(compactOp);
  // Wait for operation to complete
  operation.WaitForCompletion();
}
```

##### Async

```csharp
// Get all member nodes in the database-group using the 'GetDatabaseRecordOperation' operation
List<string> allMemberNodes =
  documentStore.Maintenance.Server.Send(new GetDatabaseRecordOperation("Northwind"))
      .Topology.Members;

// Define the compact settings as needed
CompactSettings settings = new CompactSettings
{
  // Database to compact
  DatabaseName = "Northwind",
  
  //Compact all documents in database
  Documents = true
};

// Execute the compact operation on each member node
foreach (string nodeTag in allMemberNodes)
{
  // Define the compact operation, pass the settings
  IServerOperation<OperationIdResult> compactOp = new CompactDatabaseOperation(settings);
  
  // Execute the operation on a specific node
  // Use `ForNode` to specify the node to operate on
  Operation operation = await documentStore.Maintenance.Server.ForNode(nodeTag).SendAsync(compactOp);
  // Wait for operation to complete
  await operation.WaitForCompletionAsync();
}
```

## Compaction triggers compression

- When document [compression](https://docs.ravendb.net/6.2/server/storage/documents-compression.md) is turned on, compression is applied to the documents when:
  - **New** documents that are created and saved.
  - **Existing** documents that are modified and saved.

- You can use the [compaction](https://docs.ravendb.net/6.2/client-api/operations/server-wide/compact-database.md) operation to **compress existing documents without having to modify and save** them.  
  Executing compaction triggers compression on ALL existing documents for the collections that are configured for compression.

- Learn more about Compression -vs- Compaction [here](https://docs.ravendb.net/6.2/server/storage/documents-compression.md#compression--vs--compaction).

## Compact from Studio

- Compaction can be triggered from the [Storage Report](https://docs.ravendb.net/6.2/studio/database/stats/storage-report.md) view in the Studio.  
  The operation will compact the database only on the node being viewed (node info is in the Studio footer).

- To compact the database on another node,  
  simply trigger compaction from the Storage Report view in a browser tab opened for that other node.

## Syntax

```csharp
public CompactDatabaseOperation(CompactSettings compactSettings)
```

| Parameters | Type | Description |
| - | - | - |
| **compactSettings** | `CompactSettings` | Settings for the compact operation |

| `CompactSettings` | Type | Description |
| - | - | - |
| **DatabaseName** | `string` | Name of database to compact. Mandatory param. |
| **Documents** | `bool` | Indicates if documents should be compacted. Optional param. |
| **Indexes** | `string[]` | List of index names to compact. Optional param. |
| **SkipOptimizeIndexes** | `bool` | `true` - Skip Lucene's index optimization while compacting<br />`false` - Lucene's index optimization will take place while compacting |
| | | **Note**: Either *Documents* or *Indexes* (or both) must be specified |

## See also

- [How to create database?](https://docs.ravendb.net/6.2/client-api/operations/server-wide/create-database.md)
- [How to get database statistics](https://docs.ravendb.net/6.2/client-api/operations/maintenance/get-stats.md)
- [How to restore a database from backup](https://docs.ravendb.net/6.2/client-api/operations/server-wide/restore-backup.md)
- [Switch operation to different node](https://docs.ravendb.net/6.2/client-api/operations/how-to/switch-operations-to-a-different-node.md)
- [Documents Compression](https://docs.ravendb.net/6.2/server/storage/documents-compression.md)
- [Storage Report](https://docs.ravendb.net/6.2/studio/database/stats/storage-report.md)
