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);
}
using (var store = new DocumentStore())
using (store.GetRequestExecutor().ContextPool.AllocateOperationContext(out var context))
{
    var command = new DeleteDocumentCommand("employees/1-A", null);
    await store.GetRequestExecutor().ExecuteAsync(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);
var command = new DeleteDocumentCommand("employees/1-A", null);
await asyncSession.Advanced.RequestExecutor.ExecuteAsync(command, asyncSession.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);
}
// Load a document
var employeeDocument = await asyncSession.LoadAsync<Employee>("employees/2-A");
var cv = asyncSession.Advanced.GetChangeVectorFor(employeeDocument);

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

try
{
    // Try to delete the document with the previous change-vector
    var command = new DeleteDocumentCommand("employees/2-A", cv);
    await asyncSession.Advanced.RequestExecutor.ExecuteAsync(command, asyncSession.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)
Parameter Type Description
id string The ID of the document to delete.
changeVector string The change-vector of the document you wish to delete,
used for optimistic concurrency control.
Pass null to skip the check and force the deletion.