Session: Deleting Entities

Entities can be marked for deletion by using the delete() method, but will not be removed from the server until saveChanges() is called.

Syntax

session.delete(entity);
session.delete(id, [changeVector]);
Parameters
entity object Instance of the entity to delete
id string The entity ID
changeVector string a change vector to use for concurrency checks

Example I

const employee = await session.load("employees/1");

session.delete(employee);
await session.saveChanges();

Concurrency on Delete

If useOptimisticConcurrency is set to true (default false), the delete() method will use loaded employees/1 change vector for concurrency check and might throw ConcurrencyException.

Example II

session.delete("employees/1");
await session.saveChanges();

Concurrency on Delete

In this example, the delete() method will not do any change vector based concurrency checks because the change vector for employees/1 is unknown.

Information

If entity is not tracked by session, then executing

session.delete("employees/1");

is equal to doing

session.advanced.defer(new DeleteCommandData("employees/1", null));

Change Vector in DeleteCommandData

In this sample the change vector is null - this means that there will be no concurrency checks. A non-null and valid change vector value will trigger a concurrency check.

You can read more about defer operations here.