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

# Delete by Query Operation

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/client-api/operations/common/delete-by-query.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."

`DeleteByQueryOperation` gives you the ability to delete a large number of documents with a single query.
This operation is performed in the background on the server.

## Syntax

```java
public DeleteByQueryOperation(IndexQuery queryToDelete)

public DeleteByQueryOperation(IndexQuery queryToDelete, QueryOperationOptions options)
```

| Parameters | Type | Description |
| - | - | - |
| **indexName** | String | Name of an index to perform a query on |
| **queryToDelete** | IndexQuery | Holds all the information required to query an index |
| **options** | QueryOperationOptions | Holds different setting options for base operations |

## Example I

### Java

```java
// remove all documents from the server where Name == Bob using Person/ByName index
store
  .operations()
  .send(new DeleteByQueryOperation(new IndexQuery("from Persons where name = 'Bob'")));
```

### RQL

```sql
from index 'Person/ByName' where Name = 'Bob'
```

## Example II

### Java

```java
// remove all documents from the server where Age > 35 using Person/ByAge index
store
  .operations()
  .send(new DeleteByQueryOperation(new IndexQuery("from 'Person/ByAge' where age < 35")));
```

### RQL

```sql
from index 'Person/ByName' where Age < 35
```

## Example III

### Java

```java
// delete multiple docs with specific ids in a single run without loading them into the session
Operation operation = store
  .operations()
  .sendAsync(new DeleteByQueryOperation(new IndexQuery(
      "from People u where id(u) in ('people/1-A', 'people/3-A')"
  )));
```

### RQL

```sql
from People u where id(u) in ('people/1-A', 'people/3-A')
```

> **Note:** WaitForCompletion {#waitforcompletion}
>
> `DeleteByQueryOperation` is performed in the background on the server.  
> You have the option to **wait** for it using `waitForCompletion`.
>
> ### Java
>
> ```java
> // remove all document from server where Name == Bob and Age >= 29 using People collection
> Operation operation = store.operations()
>   .sendAsync(new DeleteByQueryOperation(new IndexQuery(
>       "from People where Name = 'Bob' and Age >= 29"
>   )));
>
> operation.waitForCompletion();
> ```
>
> ### RQL
>
> ```sql
> from People where Name = 'Bob' and Age >= 29
> ```

## Remarks

> **Warning:** Map only indexes {#map-only-indexes}
>
> `DeleteByQueryOperation` can only be performed on a map index. Executing it on map-reduce index will lead to an exception.

> **Warning:** Batching and Concurrency {#batching-and-concurrency}
>
> The deletion of documents matching a specified query is run in batches of size 1024. RavenDB doesn't do concurrency checks during the operation
> so it can happen than a document has been updated or deleted meanwhile.

## See also

- [What are Operations](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.java.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.java.md)
- [What is RQL](https://docs.ravendb.net/6.2/client-api/session/querying/what-is-rql.md)
