liveBook · Manning

Delete clause

As explained in Graph Algorithms for Data Science

A Cypher clause that removes matched nodes and relationships from a graph database.

The DELETE clause removes nodes and relationships from a graph database. Use MATCH to identify the graph pattern first, then DELETE the matched elements. section 3.1.6

Use deletion queries in these ways:

  • Delete a relationship by matching its endpoint nodes and binding the relationship variable. section 3.1.6
  • Delete a node with DELETE when it has no relationships attached. section 3.1.6
  • Use DETACH DELETE when a node still has relationships; it removes those relationships before removing the node. section 3.1.6
  • Use MATCH (n) DETACH DELETE n to remove every node and relationship in the database. section 3.1.6

Deleting a relationship

MATCH (n:Person)-[r]->(m:Person) WHERE n.name = 'Satish' AND m.name = 'Elaine' DELETE r. The relationship type is omitted, so the pattern searches for any relationship between the matched people. 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 DETACH DELETE query for removing Elaine and all relationships attached to her. section 3.1.6

How Graph Algorithms for Data Science covers it

How deep it goes
Explains matching, relationship restrictions, DETACH DELETE, and whole-database deletion through executable queries.
Examples
Demonstrates deleting the Satish-Elaine relationship, deleting individual people, and clearing the entire graph.
What it assumes
Assumes basic Cypher patterns and teaches that MATCH identifies deletion targets before DELETE acts.
What you can do after
Operate a graph database by writing Cypher queries that safely delete matched nodes or relationships.
How it connects
Connects DELETE with MATCH, WHERE, relationships, nodes, and DETACH DELETE.

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.