Ryan Worsley
Ryan Worsley

Reputation: 655

Raven DB: How can I delete all documents of a given type

More specifically in Raven DB, I want to create a generic method with a signature like;

public void Clear<T>() {...

Then have Raven DB clear all documents of the given type.

I understand from other posts by Ayende to similar questions that you'd need an index in place to do this as a batch.

I think this would involve creating an index that maps each document type - this seems like a lot of work.

Does anyone know an efficient way of creating a method like the above that will do a set delete directly in the database?

Upvotes: 17

Views: 11415

Answers (4)

alexn
alexn

Reputation: 58962

I assume you want to do this from the .NET client. If so, use the standard DocumentsByEntityName index:

var indexQuery = new IndexQuery { Query = "Tag:" + collectionName };
session.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex(
   "Raven/DocumentsByEntityName", 
   indexQuery, 
   new BulkOperationOptions { AllowStale = true });

var hilo = session.Advanced.DocumentStore.DatabaseCommands.Get("Raven/H‌​ilo/", collectionName);
if (hilo != null) {
    session.Advanced.DocumentStore.DatabaseCommands.Delete(hilo.‌​Key, hilo.Etag);
}

Where collectionName is the actual name of your collection.

The first operation deletes the items. The second deletes the HiLo file.

Also check out the official documentation - How to delete or update documents using index.

Upvotes: 23

Ryan Worsley
Ryan Worsley

Reputation: 655

After much experimentation I found the answer to be quite simple, although far from obvious;

public void Clear<T>()
{
    session.Advanced.DocumentStore.DatabaseCommands.PutIndex(indexName, new IndexDefinitionBuilder<T>
    {
        Map = documents => documents.Select(entity => new {})
    });

    session.Advanced.DatabaseCommands.DeleteByIndex(indexName, new IndexQuery());
}

Of course you almost certainly wouldn't define your index and do your delete in one go, I've put this as a single method for the sake of brevity.

My own implementation defines the indexes on application start as recommended by the documentation.

If you wanted to use this approach to actually index a property of T then you would need to constrain T. For example if I have an IEntity that all my document classes inherit from and this class specifies a property Id. Then a 'where T : IEntity' would allow you to use that property in the index.

It's been said in other places, but it's also worth noting that once you define a static index Raven will probably use it, this can cause your queries to seemingly not return data that you've inserted:

RavenDB Saving to disk query

Upvotes: 9

peinearydevelopment
peinearydevelopment

Reputation: 11474

I had this problem as well and this is the solution that worked for me. I'm only working in a test project, so this might be slow for a bigger db, but Ryan's answer didn't work for me.

    public static void ClearDocuments<T>(this IDocumentSession session)
    {
        var objects = session.Query<T>().ToList();
        while (objects.Any())
        {
            foreach (var obj in objects)
            {
                session.Delete(obj);
            }

            session.SaveChanges();
            objects = session.Query<T>().ToList();
        }
    }

Upvotes: 8

Ayende Rahien
Ayende Rahien

Reputation: 22956

You can do that using: http://blog.orangelightning.co.uk/?p=105

Upvotes: 4

Related Questions