Drop an entire collection in RavenDB

Deleting a single document in RavenDB is easy. Deleting several documents is too. Deleting an entire collection of N-documents isn’t supported by default.

Here is a great extension that deletes an entire collection from your RavenDB document store:

    /// <summary>
    /// Deletes, or 'drops', an entire collection from the datastore, by the specified type.
    /// </summary>
    public void Drop<T>()
    {
        // Determine the concrete type of T
        Type g = typeof (T);

        // Delete the entire collection
        CurrentSession.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName",
            // ouch, be weary of the 's' suffix here (pluralization thingie)
            new IndexQuery { Query = "Tag:" + g.Name + "s" },
            allowStale: true
        );
    }