You are currently browsing legacy 3.5 version of documentation. Click here to switch to the newest 5.1 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: How to Delete Documents Using Index with LINQ Statment

To delete a large amount of documents answering certain criteria, we can use the DeleteByIndex method from session.Advanced

Syntax

Operation DeleteByIndex<T, TIndexCreator>(Expression<Func<T, bool>> expression)
    where TIndexCreator : AbstractIndexCreationTask, new();

Operation DeleteByIndex<T>(string indexName, Expression<Func<T, bool>> expression);
Parameters Type Description
indexName string name of an index to perform a query on
expression Expression<Func<T, bool>> The LINQ expression (the query that will be performed)
Return Value
Operation Object that allows waiting for an operation to complete.

Remarks

Note

DeleteByIndex can only be performed on a map index. Executing it on map-reduce index will lead to an exception. The document will be removed from the server after the method is called and not after SaveChanges.

Example

// remove all documents from the server where Name == Bob using Person/ByName index
var operation1 = session.Advanced.DeleteByIndex<Person>("Person/ByName", x => x.Name == "Bob");
operation1.WaitForCompletion();

//remove all documents from the server where Age > 35 using Person/ByAge index
var operation2 = session.Advanced.DeleteByIndex<Person, Person_ByAge>(x => x.Age < 35);
operation2.WaitForCompletion();