Skip to main content

Delete Document Command

  • Use the low-level DeleteDocumentCommand to remove a document from the database.

  • To delete a document using a higher-level method, see deleting entities.

  • In this page:

Examples

Delete document command - using the Store's request executor:

using (var store = new DocumentStore())
using (store.GetRequestExecutor().ContextPool.AllocateOperationContext(out var context))
{
var command = new DeleteDocumentCommand("employees/1-A", null);
store.GetRequestExecutor().Execute(command, context);
}

Delete document command - using the Session's request executor:

var command = new DeleteDocumentCommand("employees/1-A", null);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);

Delete document command - with concurrency check:

// Load a document
var employeeDocument = session.Load<Employee>("employees/2-A");
var cv = session.Advanced.GetChangeVectorFor(employeeDocument);

// Modify the document content and save changes
// The change-vector of the stored document will change
employeeDocument.Title = "Some new title";
session.SaveChanges();

try
{
// Try to delete the document with the previous change-vector
var command = new DeleteDocumentCommand("employees/2-A", cv);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
}
catch (Exception e)
{
// A concurrency exception is thrown
// since the change-vector of the document in the database
// does not match the change-vector specified in the delete command
Assert.IsType<Raven.Client.Exceptions.ConcurrencyException>(e);
}

Syntax

public DeleteDocumentCommand(string id, string changeVector)
ParameterTypeDescription
idstringThe ID of the document to delete.
changeVectorstringThe change-vector of the document you wish to delete,
used for optimistic concurrency control.
Pass null to skip the check and force the deletion.