11

I need to delete a whole collection of documents in Raven DB. Deleting one by one (documents) is not a wise choice. Is there a way I can do this easily?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ZVenue
  • 4,967
  • 16
  • 61
  • 92

2 Answers2

6

You can do a set based operation.

store.DatabaseCommands.DeleteByIndex() to do so

store.DatabaseCommands.DeleteByIndex(
    "Enquiries/MyEnquiryIndexName",
    new IndexQuery { Query = "Id:*", },
    allowStale: false);

Code sample by @Marijin

SandRock
  • 5,276
  • 3
  • 30
  • 49
Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
1

Not sure about previous versions, but below applies to RavenDB 5.0

If you wanted to delete all documents from a collection called "Users", you could pass in the collection name to the DeleteByQueryOperation

DeleteByQueryOperation("from Users")

A generic version would look something like this:

using Raven.Client.Documents;
using Raven.Client.Documents.Operations;

public class ExampleClass
{
  public static void DeleteCollection<TEntity>(IDocumentStore store, string databaseName)
  {
    var collectionName = store.Conventions.GetCollectionName(typeof(TEntity));

    store.Operations
      .ForDatabase(databaseName)
      .Send(new DeleteByQueryOperation($"from {collectionName}"));
  }
}
jnt
  • 1,133
  • 14
  • 10