liveBook · Manning

Detach delete clause

As explained in Graph Algorithms for Data Science

A DETACH DELETE clause is a Cypher deletion clause that deletes a node together with its existing relationships.

DETACH DELETE removes a node and first deletes every relationship attached to it. Use it when ordinary DELETE cannot remove a connected node. section 3.1.6

Delete Elaine and her relationships

MATCH (n:Person) WHERE n.name = 'Elaine' DETACH DELETE n This query removes the Elaine node together with its attached relationships. section 3.1.6

Use DETACH DELETE when:

  • A node still has relationships and ordinary DELETE returns an error. section 3.1.6
  • You deliberately want to remove all nodes and relationships by matching every node. section 3.1.6
Listing 3.27 Cypher statement that deletes a single node and all its relationships from the database by using the DETACH DELETE clause
MATCH (n:Person)
WHERE n.name = 'Elaine'
DETACH DELETE n

Inspect the complete Cypher statement for deleting one node and all its relationships. section 3.1.6

How Graph Algorithms for Data Science covers it

How deep it goes
Explains the deletion order and demonstrates single-node and whole-database queries, without covering optimization details.
Examples
Uses Elaine’s connected Person node and a query matching every node to demonstrate targeted and whole-database deletion.
What it assumes
Assumes familiarity with MATCH and basic node and relationship deletion.
What you can do after
Operate/configure Cypher queries that remove connected nodes or clear all nodes and relationships.
How it connects
Connects DETACH DELETE with DELETE and MATCH for selecting nodes and removing graph elements.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.