You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Deleting entities

Entities can be marked for deletion by using Delete method, but will not be removed from server until SaveChanges is called.

Syntax

void Delete<T>(T entity);

void Delete<T>(ValueType id);

void Delete(string id);

Parameters
entity or id T, ValueType or string instance of entity to delete or entity Id

Example 1

// if UseOptimisticConcurrency is set to 'true' (default 'false')
// this 'Delete' method will use loaded 'employees/1' etag for concurrency check
// and might throw ConcurrencyException
Employee employee = session.Load<Employee>("employees/1");
session.Delete(employee);
session.SaveChanges();

Example 2

// this 'Delete' method will not do any Etag-based concurrency checks
// because Etag for 'employees/1' is unknown
session.Delete("employees/1");
session.SaveChanges();

Information

If entity is not tracked by session, then executing

session.Delete("employees/1");

is equal to doing

session.Advanced.Defer(new DeleteCommandData { Key = "employees/1" });

You can read more about defer operations here.