DELETE

The DELETE clause is used to delete nodes, relationships or paths.

For removing properties and labels, see the REMOVE clause.

It is not possible to delete nodes with relationships connected to them without also deleting the relationships. This can be done by either explicitly deleting specific relationships, or by using the DETACH DELETE clause.

Example graph

The following graph is used for the examples below. It shows four actors, three of whom ACTED_IN the Movie The Matrix (Keanu Reeves, Carrie-Anne Moss, and Laurence Fishburne), and one actor who did not act in it (Tom Hanks).

graph delete clause

To recreate the graph, run the following query in an empty Neo4j database:

CREATE
  (keanu:Person {name: 'Keanu Reever'}),
  (laurence:Person {name: 'Laurence Fishburne'}),
  (carrie:Person {name: 'Carrie-Anne Moss'}),
  (tom:Person {name: 'Tom Hanks'}),
  (theMatrix:Movie {title: 'The Matrix'}),
  (keanu)-[:ACTED_IN]->(theMatrix),
  (laurence)-[:ACTED_IN]->(theMatrix),
  (carrie)-[:ACTED_IN]->(theMatrix)

Delete single node

To delete a single node, use the DELETE clause:

Query
MATCH (n:Person {name: 'Tom Hanks'})
DELETE n

This deletes the Person node Tom Hanks. This query is only possible to run on nodes without any relationships connected to them.

Result
Deleted 1 node

Delete relationships only

It is possible to delete a relationship while leaving the node(s) connected to that relationship otherwise unaffected.

Query
MATCH (n:Person {name: 'Laurence Fishburne'})-[r:ACTED_IN]->()
DELETE r

This deletes all outgoing ACTED_IN relationships from the Person node Laurence Fishburne, without deleting the node.

Result
Deleted 1 relationship

Delete a node with all its relationships

To delete nodes and any relationships connected them, use the DETACH DELETE clause.

Query
MATCH (n:Person {name: 'Carrie-Anne Moss'})
DETACH DELETE n

This deletes the Person node Carrie-Anne Moss and all relationships connected to it.

Result
Deleted 1 node, deleted 1 relationship

The DETACH DELETE clause may not be permitted to users with restricted security privileges. For more information, see Operations Manual → Fine-grained access control.

Delete all nodes and relationships

It is possible to delete all nodes and relationships in a graph.

Query
MATCH (n)
DETACH DELETE n
Result
Deleted 3 nodes, deleted 1 relationship

This query is not for deleting large amounts of data, but is useful when experimenting with small example datasets. When deleting large amounts of data, instead use CALL { …​ } IN TRANSACTIONS.