Thursday, January 3, 2013

RavenDb: Delete al documents from a specific type

Last week I was looking for a way to delete all documents of a specific type from the database. For example you want to remove all your Orders from the database.

I started implementing this by loading all documents and delete them one by one. But this was a rather inefficient solution, so I started to look at some alternatives.

After some attempts, I ended up with the following code:

var documentName="Orders";

session.Advanced.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName",
new IndexQuery { Query = "Tag:" + documentName },
allowStale: true
);


Clean, simple AND efficient!

No comments: