Skip to content

Deleting Data

The object manager provides type-safe methods for deleting graph objects.

// Delete a single node by UUID
val deleted = graphObjectManager.delete<Person>(uuid)
// Delete a GraphView's root node (relationships are detached)
graphObjectManager.delete<RaisedAndAssignedIssue>(issueUuid)
// Delete only if condition is met
graphObjectManager.delete<Issue>(uuid, "n.state = 'closed'")
// For GraphViews, use the root fragment alias
graphObjectManager.delete<RaisedAndAssignedIssue>(uuid, "issue.state = 'closed'")
// Delete all matching a condition
graphObjectManager.deleteAll<Issue>("n.state = 'closed'")
// For GraphViews
graphObjectManager.deleteAll<RaisedAndAssignedIssue>("issue.locked = true")

The most powerful way - uses generated DSL for compile-time type checking:

// Delete closed issues
graphObjectManager.deleteAll<RaisedAndAssignedIssue> {
where {
issue.state eq "closed"
}
}
// Delete with multiple conditions
graphObjectManager.deleteAll<RaisedAndAssignedIssue> {
where {
issue.state eq "open"
issue.locked eq true
}
}
// Delete by relationship property
graphObjectManager.deleteAll<RaisedAndAssignedIssue> {
where {
assignedTo.name eq "Former Employee"
}
}
// Delete all (no filter)
graphObjectManager.deleteAll<RaisedAndAssignedIssue> { }

A delete with no cascade uses DETACH DELETE:

  • Removes the node and all its relationships
  • Related nodes are not deleted (only the relationships to them); a cascade, below, deletes them too
  • Returns the count of deleted nodes
// Delete an issue - persons remain, only ASSIGNED_TO/RAISED_BY relationships removed
graphObjectManager.delete<RaisedAndAssignedIssue>(issueUuid)
// Verify related nodes still exist
val person = graphObjectManager.load<Person>(personUuid) // Still there!

Deleting a @GraphView by id can also delete the nodes the view reaches:

graphObjectManager.delete<SessionView>(sessionId) // NONE: the root only
graphObjectManager.delete<SessionView>(sessionId, CascadeType.DELETE_ORPHAN) // and each node in the view that nothing outside it still needs
graphObjectManager.delete<SessionView>(sessionId, CascadeType.DELETE_ALL) // and every node in the view
  • The cascade follows the view’s declared relationships, through nested views. A node outside the view is never deleted.
  • A view that contains itself (val parts: List<SessionTree>, with a maxDepth) deletes what a load of it reads: the nodes at every level, and what each level holds through the view’s other relationships.
  • A cascade never stops short. If a recursive relationship reaches below its maxDepth, the delete throws IllegalStateException before anything is written, saying how deep the view reaches; raise the maxDepth to cover the deepest level.
  • DELETE_ALL permanently deletes nodes that other nodes may still point at. Use it for nodes the root owns.
  • DELETE_ORPHAN keeps a node in the view that a node outside it still refers to, at any level, together with everything the view holds below it: a bookmarked message keeps its attachments, and a pinned sub-session keeps its messages and its own sub-sessions. The rest of the view is deleted, including the session that held a kept message.
  • DELETE_ORPHAN is not available on Memgraph.