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?
            Asked
            
        
        
            Active
            
        
            Viewed 1,839 times
        
    2 Answers
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
- 
                    5Can we get an example? I'm spacing on this and just cannot figure it out. – Bobby Cannon Mar 12 '12 at 19:34
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
 
    