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 left with no relationship
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.
  • DELETE_ALL permanently deletes nodes that other nodes may still point at. Use it for nodes the root owns.
  • DELETE_ORPHAN is not available on Memgraph.