1
votes

I saw the examples of deleting collection entities by query;
https://ravendb.net/docs/article-page/4.0/Csharp/client-api/operations/delete-by-query

But my question is, how to delete a collection without giving an index ?

An example, I created/insert a collection to RavenDB like this;

using (IDocumentSession session = _documentStore.OpenSession())
{
    session.Store<TEntity>(entity);
    session.SaveChanges();
}

I didn't created any index. Just stored some data to RavenDB. And I looked api for something like this;

using (IDocumentSession session = _documentStore.OpenSession())
{
    session.DeleteAll<TEntity>()
    session.SaveChanges();
}

But this does not exists. So how to delete entities without given an index ?

1

1 Answers

4
votes

This following code would run directly on the collection without the need to create an index:

var queryToDelete = new IndexQuery { Query = $"FROM {collection}" };
var operation = store.Operations.Send(new DeleteByQueryOperation(queryToDelete, new QueryOperationOptions { AllowStale = false }));
operation.WaitForCompletion(TimeSpan.FromSeconds(60));