---
title: "Set Index Lock Mode Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/set-index-lock"
version: "6.2"
language: "csharp"
---

# Set Index Lock Mode Operation

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/set-index-lock.java.md), [Python](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/set-index-lock.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/set-index-lock.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/set-index-lock.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**
>
> - The lock mode controls the behavior of index modifications.  
>   Use `SetIndexesLockOperation` to modify the **lock mode** for a single index or multiple indexes.
>
> - **Indexes scope**:  
>   The lock mode can be set only for static-indexes, not for auto-indexes.
>
> - **Nodes scope**:  
>   The lock mode will be updated on all nodes in the database group.
>
> - Setting the lock mode can also be done in the **Studio** [indexes list](https://docs.ravendb.net/6.2/studio/database/indexes/indexes-list-view.md#indexes-list-view---actions) view.  
>   Locking an index is not a security measure, the index can be unlocked at any time.
>
> - In this page:
>   - [Lock modes](#lock-modes)
>   - [Sample usage flow](#sample-usage-flow)
>   - [Set lock mode - single index](#set-lock-mode---single-index)
>   - [Set lock mode - multiple indexes](#set-lock-mode---multiple-indexes)
>   - [Syntax](#syntax)

## Lock modes

- **Unlocked** - when lock mode is set to `Unlock`:
  - Any change to the index definition will be applied.
  - If the new index definition differs from the one stored on the server,  
    the index will be updated and the data will be re-indexed using the new index definition.
  - The index can be deleted.

- **Locked (ignore)** - when lock mode is set to `LockedIgnore`:
  - Index definition changes will Not be applied.
  - Modifying the index definition will return successfully and no error will be raised,  
    however, no change will be made to the index definition on the server.
  - Trying to delete the index will not remove it from the server, and no error will be raised.

- **Locked (error)** - when lock mode is set to `LockedError`:
  - Index definitions changes will Not be applied.
  - An exception will be thrown upon trying to modify or delete the index.
  - The index cannot be deleted. Attempting to do so will result in an exception.

## Sample usage flow

Consider the following scenario:

- Your client application defines and [deploys a static-index](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/put-indexes.md) upon application startup.

- After the application has started, you make a change to your index definition and re-indexing occurs.  
  However, if the index lock mode is *'Unlock'*, the next time your application will start,  
  it will reset the index definition back to the original version.

- Locking the index allows to make changes to the running index and prevents the application  
  from setting it back to the previous definition upon startup. See the following steps:

1. Run your application
2. Modify the index definition on the server (from Studio, or from another application),  
   and then set this index lock mode to `LockedIgnore`.
3. A side-by-side replacement index is created on the server.  
   It will index your dataset according to the **new** definition.
4. At this point, if any instance of your original application is started,  
   the code that defines and deploys the index upon startup will have no effect  
   since the index is 'locked'.
5. Once the replacement index is done indexing, it will replace the original index.

## Set lock mode - single index

### Sync

```csharp
// Define the set lock mode operation
// Pass index name & lock mode
var setLockModeOp = new SetIndexesLockOperation("Orders/Totals", IndexLockMode.LockedIgnore);

// Execute the operation by passing it to Maintenance.Send
// An exception will be thrown if index does not exist
store.Maintenance.Send(setLockModeOp);

// Lock mode is now set to 'LockedIgnore'
// Any modifications done now to the index will Not be applied, and will Not throw
```

### Async

```csharp
// Define the set lock mode operation
// Pass index name & lock mode
var setLockModeOp = new SetIndexesLockOperation("Orders/Totals", IndexLockMode.LockedIgnore);

// Execute the operation by passing it to Maintenance.SendAsync
// An exception will be thrown if index does not exist
await store.Maintenance.SendAsync(setLockModeOp);

// Lock mode is now set to 'LockedIgnore'
// Any modifications done now to the index will Not be applied, and will Not throw
```

## Set lock mode - multiple indexes

### Sync

```csharp
// Define the index list and the new lock mode:
var parameters = new SetIndexesLockOperation.Parameters {
  IndexNames = new[] {"Orders/Totals", "Orders/ByCompany"}, 
  Mode = IndexLockMode.LockedError
};
  
// Define the set lock mode operation, pass the parameters
var setLockModeOp = new SetIndexesLockOperation(parameters);

// Execute the operation by passing it to Maintenance.Send
// An exception will be thrown if any of the specified indexes do not exist
store.Maintenance.Send(setLockModeOp);

// Lock mode is now set to 'LockedError' on both indexes
// Any modifications done now to either index will throw
```

### Async

```csharp
// Define the index list and the new lock mode:
var parameters = new SetIndexesLockOperation.Parameters {
  IndexNames = new[] {"Orders/Totals", "Orders/ByCompany"}, 
  Mode = IndexLockMode.LockedError
};

// Define the set lock mode operation, pass the parameters
var setLockModeOp = new SetIndexesLockOperation(parameters);
              
// Execute the operation by passing it to Maintenance.SendAsync
// An exception will be thrown if any of the specified indexes do not exist
await store.Maintenance.SendAsync(setLockModeOp);

// Lock mode is now set to 'LockedError' on both indexes
// Any modifications done now to either index will throw
```

## Syntax

```csharp
// Available overloads:
public SetIndexesLockOperation(string indexName, IndexLockMode mode);
public SetIndexesLockOperation(Parameters parameters);
```

| Parameters | Type | Description |
| - | - | - |
| **indexName** | string | Index name for which to set lock mode |
| **mode** | `IndexLockMode` | Lock mode to set |
| **parameters** | `SetIndexesLockOperation.Parameters` | List of indexes + Lock mode to set.<br />An exception is thrown if any of the specified indexes do not exist. |

```csharp
public enum IndexLockMode
{
  Unlock,
  LockedIgnore,
  LockedError
}
```

```csharp
public class Parameters
{
  public string[] IndexNames { get; set; }
  public IndexLockMode Mode { get; set; }
}
```

## See also

- [What are Indexes](https://docs.ravendb.net/6.2/indexes/what-are-indexes.md)
- [Creating and Deploying Indexes](https://docs.ravendb.net/6.2/indexes/creating-and-deploying.md)
- [Index Administration](https://docs.ravendb.net/6.2/indexes/index-administration.md)
- [How to Change Index Priority](https://docs.ravendb.net/6.2/client-api/operations/maintenance/indexes/set-index-priority.md)
