Thursday, January 24, 2013

RavenDB: Drop all collections

I never stub or mock the database when I'm using RavenDB. Generally, I use an embeddable documentstore running in memory, and initialize a new instance on every test. However, I like to run some stress tests against a real instance, and here I found myself wanting to wipe clean the state of previous tests, without having to create a new database (which is rather slow).

First I create the default DocumentsByEntityName index to make sure it's there - it normally gets created when you open the studio for the first time. Then I use one of the advanced database commands: DeleteByIndex, and query all the tags.
using (var session = _documentStore.OpenSession())
    new RavenDocumentsByEntityName().Execute(_documentStore);
            new IndexQuery { Query = "Tag: *" });                
This technique doesn't seem to be widely used judging by the first page of Google search results. If there is a reason for that though, let me know!


  1. You should delete the Data folder - delete by query is going to take time and will trigger unnecessary index updates.

    1. What if the server is not on my machine? What about file locks?

  2. And for the visual basic (vb .net) afficionados:

    Dim _index As New Raven.Client.Indexes.RavenDocumentsByEntityName

    RavenDBStore.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName", New Raven.Abstractions.Data.IndexQuery() With {.Query = "Tag: *"}, True)

    Notice that "allow stale" is set to true. When running this for the first time maybe not all docs will be deleted (because not yet indexed). Haven't read the ravendb docs for that yet, but will comment again when I find out.