Skip to main content

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(string id);

void Delete(string id, string expectedChangeVector);
Parameters
entity or idT, ValueType or stringinstance of entity to delete or entity Id

Example 1

Employee employee = session.Load<Employee>("employees/1");

session.Delete(employee);
session.SaveChanges();

Example 2

session.Delete("employees/1");
session.SaveChanges();

If entity is not tracked by session, then executing

session.Delete("employees/1");

is equal to doing

session.Advanced.Defer(new DeleteCommandData("employees/1", changeVector: null));

You can read more about defer operations here.