17

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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Ryan Worsley
  • 655
  • 1
  • 4
  • 17

4 Answers4

23

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.

Alex Klaus
  • 8,168
  • 8
  • 71
  • 87
alexn
  • 57,867
  • 14
  • 111
  • 145
  • 1
    this is imho a better way than the accepted answer as it does not require the creation of an index and it deletes any docs that may not be included if you were deleting using an existing index. – wal Nov 11 '14 at 00:22
  • I also like to delete the related HiLo document to reset the IDs. @alexn can I edit your code to add this? `var hilo = session.Advanced.DocumentStore.DatabaseCommands.Get("Raven/Hilo/themes"); session.Advanced.DocumentStore.DatabaseCommands.Delete(hilo.Key, hilo.Etag);` – SandRock Oct 28 '15 at 10:16
  • @SandRock absolutely :) – alexn Oct 28 '15 at 13:21
  • Above code has some syntax errors. Here is error free code 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); } – Sajid Ali Oct 03 '17 at 11:53
9

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

Community
  • 1
  • 1
Ryan Worsley
  • 655
  • 1
  • 4
  • 17
  • 2
    As a RavenDB contributor, I don't recommend this way, as you're creating an index needlessly. Instead, I recommend @alexn's answer: http://stackoverflow.com/a/13049179/536 – Judah Gabriel Himango May 03 '16 at 14:04
8

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();
        }
    }
peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76
  • I'm also running tests against an Embedded store and only your solution worked for me. A post by on Google Groups (https://groups.google.com/forum/#!topic/ravendb/QqZPrRUwEkE) suggests you need to first create the DocumentsByEntityName index in this scenario: "new RavenDocumentsByEntityName().Execute(store);" Having done this I still found the documents that should have been deleted were still present (I also prevent stale queries being returned so this wasn't the answer either). – rogersillito Feb 25 '15 at 18:26
4

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

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • Hi Ayende, your answer was realy useful, but not quite what I was looking for. After much experimentation and learning about Raven I found the solution I was looking for. I'll post the answer here shortly for others. – Ryan Worsley Apr 19 '12 at 06:12
  • I found this to be the easiest non-programmatic solution. – Keith May 17 '13 at 17:44
  • I never found the Reset command mentioned in that blog. Ended up with an index similar to Ryan's. – Nils Magne Lunde Dec 23 '13 at 11:34