You are currently browsing legacy 3.5 version of documentation. Click here to switch to the newest 5.1 version.
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
Operation DeleteByIndex<T, TIndexCreator>(Expression<Func<T, bool>> expression) where TIndexCreator : AbstractIndexCreationTask, new(); Operation DeleteByIndex<T>(string indexName, Expression<Func<T, bool>> expression);
|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)|
|Operation||Object that allows waiting for an operation to complete.|
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
// 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();