RavenDB version 2.5. Other versions:

Deleting documents

Delete by an object reference

Once a valid reference to a document has been retrieved, the document can be deleted with a call to Delete in the following manner:

    session.Delete(existingBlogPost);
    session.SaveChanges();
    

Deletes are final and cannot be rolled back, once committed.

Delete by ID

If you have the document's ID, and you don't want to load it just for the sake of delete it, you can either defer deleting it using the Defer command or doing a direct delete using the DatabaseCommands.

Delete by ID using the Defer command

Using the Defer command in the Advanced section of the session API, you can pass a DeleteCommandData instance which will instruct the session to delete the document upon the call to the SaveChanges method. This will ensure that the delete will be transactional because of its participation in the Unit Of Work of the session.

Here is an example how to use it:

    session.Advanced.Defer(new DeleteCommandData { Key = "posts/1234" });
    

Delete by ID using the DatabaseCommands

You can perform a direct delete using the DatabaseCommands property available in the Advanced section of the session API.

    session.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null);
    

Unlike session operations, DatabaseCommands operations execute immediately, and do not wait for SaveChanges to be called.

Comments add new comment

The comments section is for user feedback or community content. If you seek assistance or have any questions, please post them at our support forums.

Serge van den Oever
REPLY Posted by Serge van den Oever on

To use session.Advanced.Defer(new DeleteCommandData {Key = "posts/1234"}): - Add a reference to Raven.Abstractions - Add the following code line: using Raven.Abstractions.Commands;

Daniel Lidström
REPLY Posted by Daniel Lidström on

session.Advanced.DatabaseCommands.Delete("posts/1234", null) seems to be obsolete in Raven 1.2 unstable. What's the new syntax?

Fitzchak Yitzchaki
REPLY Posted by Fitzchak Yitzchaki on

session.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null)

Simon Cropp
REPLY Posted by Simon Cropp on

I know session.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null); is technically correct

but it is a little misleading since the example makes it look like it acts off the session and hence should be transactional. Which is not the case "Unlike session operations, DatabaseCommands operations execute immediately, and do not wait for SaveChanges to be called."

perhaps in an attempt to avoid confusion change the example to

documentStore.DatabaseCommands.Delete("posts/1234", null);

or add a comment in the indented code so it looks like this

// Not Transitional
session.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null);

Fitzchak Yitzchaki
REPLY Posted by Fitzchak Yitzchaki on

Yes, it is confusing some of our users. But if you think about this, this is just a link to the document store from the session. You are using the document store, and not the session.

Andrew
REPLY Posted by Andrew on

Is there a way to delete whole catalogue/tables at once?

SUBMIT COMMENT