---
title: "Delete by Query Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/common/delete-by-query"
version: "6.2"
language: "csharp"
---

# Delete by Query Operation

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/operations/common/delete-by-query.java.md), [Python](https://docs.ravendb.net/6.2/client-api/operations/common/delete-by-query.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/operations/common/delete-by-query.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/common/delete-by-query.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 `DeleteByQueryOperation` to delete a large number of documents that match the provided query in a single server call.
>
> - **Dynamic behavior**:  
>   The deletion of documents matching the specified query is performed in batches of size 1024.  
>   During the deletion process, documents that are added/modified **after** the delete operation has started  
>   may also be deleted if they match the query criteria.
>
> - **Background operation**:  
>   This operation is performed in the background on the server.  
>   If needed, you can wait for the operation to complete. See: [Wait for completion](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.md#wait-for-completion).
>
> - **Operation scope**:  
>   `DeleteByQueryOperation` runs as a single-node transaction, not a cluster-wide transaction. As a result,  
>   if you use this operation to delete documents that were originally created using a cluster-wide transaction,  
>   their associated [Atomic guards](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/atomic-guards.md) will Not be deleted.
>
>   - To avoid issues when recreating such documents using a cluster-wide session,
>     see [Best practice when storing a document](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/atomic-guards.md#best-practice-when-storing-a-document-in-a-cluster-wide-transaction).
>   - To learn more about the differences between transaction types,
>     see [Cluster-wide transaction vs. Single-node transaction](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/overview.md#cluster-wide-transaction-vs-single-node-transaction).
>
> - In this article:
>   - [Delete by dynamic query](#delete-by-dynamic-query)
>   - [Delete by index query](#delete-by-index-query)
>   - [Syntax](#syntax)

## Delete by dynamic query

> **Note**
>
> ##### Delete all documents in a collection
>
> ###### DeleteByQueryOperation\_Sync
>
> ```csharp
> // Define the delete by query operation, pass an RQL querying a collection
> var deleteByQueryOp = new DeleteByQueryOperation("from 'Orders'");
>
> // Execute the operation by passing it to Operations.Send
> var operation = store.Operations.Send(deleteByQueryOp);
>
> // All documents in collection 'Orders' will be deleted from the server.
> ```
>
> ###### DeleteByQueryOperation\_Async
>
> ```csharp
> // Define the delete by query operation, pass an RQL querying a collection
> var deleteByQueryOp = new DeleteByQueryOperation("from 'Orders'");
>
> // Execute the operation by passing it to Operations.SendAsync
> var result = await store.Operations.SendAsync(deleteByQueryOp);
>
> // All documents in collection 'Orders' will be deleted from the server.
> ```
>
> ###### RQL
>
> ```sql
> from "Orders"
> ```

> **Note**
>
> ##### Delete with filtering
>
> ###### DeleteByQueryOperation\_Sync
>
> ```csharp
> // Define the delete by query operation, pass an RQL querying a collection
> var deleteByQueryOp = new DeleteByQueryOperation("from 'Orders' where Freight > 30");
>
> // Execute the operation by passing it to Operations.Send
> var operation = store.Operations.Send(deleteByQueryOp);
>
> // * All documents matching the specified RQL will be deleted from the server.
>
> // * Since the dynamic query was made with a filtering condition,
> //   an auto-index is generated (if no other matching auto-index already exists).
> ```
>
> ###### DeleteByQueryOperation\_Async
>
> ```csharp
> // Define the delete by query operation, pass an RQL querying a collection
> var deleteByQueryOp = new DeleteByQueryOperation("from 'Orders' where Freight > 30");
>
> // Execute the operation by passing it to Operations.SendAsync
> var result = await store.Operations.SendAsync(deleteByQueryOp);
>
> // * All documents matching the provided RQL will be deleted from the server.
>
> // * Since a dynamic query was made with a filtering condition,
> //   an auto-index is generated (if no other matching auto-index already exists).
> ```
>
> ###### RQL
>
> ```sql
> from "Orders" where Freight > 30
> ```

## Delete by index query

- `DeleteByQueryOperation` can only be performed on a **Map-index**.  
  An exception is thrown when executing the operation on a Map-Reduce index.

- A few overloads are available, see the following examples:

> **Note**
>
> ##### A sample Map-index
>
> ```csharp
> // The index definition:
> // =====================
>
> public class Products_ByPrice : AbstractIndexCreationTask<Product>
> {
>   public class IndexEntry
>   {
>       public decimal Price { get; set; }  
>   }
>   
>   public Products_ByPrice()
>   {
>       Map = products => from product in products
>           select new IndexEntry
>           {
>               Price = product.PricePerUnit 
>           };
>   }
> }
> ```

> **Note**
>
> ##### Delete documents via an index query
>
> ###### DeleteByQueryOperation
>
> ```csharp
> // Define the delete by query operation, pass an RQL querying the index
> var deleteByQueryOp =
>   new DeleteByQueryOperation("from index 'Products/ByPrice' where Price > 10");
>
> // Execute the operation by passing it to Operations.Send
> var operation = store.Operations.Send(deleteByQueryOp);
>
> // All documents with document-field PricePerUnit > 10 will be deleted from the server.
> ```
>
> ###### Overload\_1
>
> ```csharp
> // Define the delete by query operation
> var deleteByQueryOp = new DeleteByQueryOperation(new IndexQuery
> {
>   // Provide an RQL querying the index
>   Query = "from index 'Products/ByPrice' where Price > 10"
> });
>
> // Execute the operation by passing it to Operations.Send
> var operation = store.Operations.Send(deleteByQueryOp);
>
> // All documents with document-field PricePerUnit > 10 will be deleted from the server.
> ```
>
> ###### Overload\_2
>
> ```csharp
> // Define the delete by query operation
> var deleteByQueryOp =
>   // Pass parameters:
>   // * The index name
>   // * A filtering expression on the index-field
>   new DeleteByQueryOperation<Products_ByPrice.IndexEntry>("Products/ByPrice",
>       x => x.Price > 10);
>
> // Execute the operation by passing it to Operations.Send
> var operation = store.Operations.Send(deleteByQueryOp);
>
> // All documents with document-field PricePerUnit > 10 will be deleted from the server.
> ```
>
> ###### Overload\_3
>
> ```csharp
> // Define the delete by query operation
> var deleteByQueryOp =
>   // Pass param:
>   // * A filtering expression on the index-field
>   new DeleteByQueryOperation<Products_ByPrice.IndexEntry, Products_ByPrice>(
>       x => x.Price > 10);
>
> // Execute the operation by passing it to Operations.Send
> var operation = store.Operations.Send(deleteByQueryOp);
>
> // All documents with document-field PricePerUnit > 10 will be deleted from the server.
> ```
>
> ###### RQL
>
> ```sql
> from index "Products/ByPrice" where Price > 10
> ```

> **Note**
>
> ##### Delete with options
>
> ###### DeleteByQueryOperation
>
> ```csharp
> // Define the delete by query operation
> var deleteByQueryOp = new DeleteByQueryOperation(
>   // QUERY: Specify the query
>   new IndexQuery
>   {
>       Query = "from index 'Products/ByPrice' where Price > 10"
>   },
>   // OPTIONS: Specify the options for the operation
>   // (See all other available options in the Syntax section below)
>   new QueryOperationOptions
>   {
>       // Allow the operation to operate even if index is stale
>       AllowStale = true, 
>       // Get info in the operation result about documents that were deleted
>       RetrieveDetails = true 
>   });
>
> // Execute the operation by passing it to Operations.Send
> Operation operation = store.Operations.Send(deleteByQueryOp);
>   
> // Wait for operation to complete
> var result = operation.WaitForCompletion<BulkOperationResult>(TimeSpan.FromSeconds(15));
>
> // * All documents with document-field PricePerUnit > 10 will be deleted from the server.
>
> // * Details about deleted documents are available:
> var details = result.Details;
> var documentIdThatWasDeleted = details[0].ToJson()["Id"];
> ```
>
> ###### DeleteByQueryOperation\_async
>
> ```csharp
> // Define the delete by query operation
> var deleteByQueryOp = new DeleteByQueryOperation(
>   // QUERY: Specify the query
>   new IndexQuery
>   {
>       Query = "from index 'Products/ByPrice' where Price > 10"
>   },
>   // OPTIONS: Specify the options for the operation
>   // (See all other available options in the Syntax section below)
>   new QueryOperationOptions
>   {
>       // Allow the operation to operate even if index is stale
>       AllowStale = true, 
>       // Get info in the operation result about documents that were deleted
>       RetrieveDetails = true 
>   });
>
> // Execute the operation by passing it to Operations.Send
> Operation operation = await store.Operations.SendAsync(deleteByQueryOp);
>
> // Wait for operation to complete
> BulkOperationResult result = 
>   await operation.WaitForCompletionAsync<BulkOperationResult>(TimeSpan.FromSeconds(15))
>       .ConfigureAwait(false);
>
> // * All documents with document-field PricePerUnit > 10 will be deleted from the server.
>
> // * Details about deleted documents are available:
> var details = result.Details;
> var documentIdThatWasDeleted = details[0].ToJson()["Id"];
> ```
>
> ###### RQL
>
> ```sql
> from index "Products/ByPrice" where Price > 10
> ```
>
> - Specifying `QueryOperationOptions` is also supported by the other overload methods, see the Syntax section below.

## Syntax

```csharp
// Available overload:
// ===================

DeleteByQueryOperation DeleteByQueryOperation(
  string queryToDelete);

DeleteByQueryOperation DeleteByQueryOperation(
  IndexQuery queryToDelete, 
  QueryOperationOptions options = null);

DeleteByQueryOperation DeleteByQueryOperation<TEntity>(
  string indexName, 
  Expression<Func<TEntity, bool>> expression,
  QueryOperationOptions options = null);

DeleteByQueryOperation DeleteByQueryOperation<TEntity, TIndexCreator>(
  Expression<Func<TEntity, bool>> expression,
  QueryOperationOptions options = null)
  where TIndexCreator : AbstractIndexCreationTask, new();
```

| Parameter | Type | Description |
| - | - | - |
| **queryToDelete** | string | The RQL query to perform |
| **queryToDelete** | `IndexQuery` | Holds all the information required to query an index |
| **indexName** | string | The name of the index queried |
| **expression** | `Expression<Func<T, bool>>` | The expression that defines the query criteria |
| **options** | `QueryOperationOptions` | Object holding different setting options for the operation |

```csharp
public class QueryOperationOptions
{
  // Indicates whether operations are allowed on stale indexes.
  // DEFAULT: false
  public bool AllowStale { get; set; }
  
  // If AllowStale is set to false and index is stale, 
  // then this is the maximum timeout to wait for index to become non-stale. 
  // If timeout is exceeded then exception is thrown.
  // DEFAULT: null (if index is stale then exception is thrown immediately) 
  public TimeSpan? StaleTimeout { get; set; }
  
  // Limits the number of base operations per second allowed.
  // DEFAULT: no limit
  public int? MaxOpsPerSecond
  
  // Determines whether operation details about each document should be returned by server.
  // DEFAULT: false
  public bool RetrieveDetails { get; set; }
  
  // Ignore the maximum number of statements a script can execute.
  // Note: this is only relevant for the PatchByQueryOperation. 
  public bool IgnoreMaxStepsForScript { get; set; }
}
```

## See also

- [What are Operations](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.md)
- [How to Query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [Querying: Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.md)
- [What is RQL](https://docs.ravendb.net/6.2/client-api/session/querying/what-is-rql.md)
