Session: Deleting Entities

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


def delete(self, key_or_entity: Union[str, object], expected_change_vector: Optional[str] = None) -> None:
key_or_entity str or object ID of the document or instance of the entity to delete
expected_change_vector str a change vector to use for concurrency checks

Example I

employee = session.load("employees/1")


Concurrency on Delete

If use_optimistic_concurrency 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


Concurrency on Delete

The delete() method will not do any change vector based concurrency checks because the change vector for 'employees/1' is unknown.


If entity is not tracked by session, then executing:


is equal to doing:

session.advanced.defer(DeleteCommandData("employees/1", change_vector=None))

Change Vector in DeleteCommandData

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

You can read more about defer operations here.