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.

While the DELETE clause renders the deleted objects no longer accessible, the space occupied by the deleted nodes and relationships remain on the disk and is reserved for future transactions creating data. For information about how to clear and reuse the space occupied by deleted objects, see Operations Manual → Space reuse.

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

NODETACH keyword

It is also possible to delete the single node using the NODETACH DELETE clause. Using the NODETACH keyword explicitly defines that relationships will not be detached and deleted from a node. The NODETACH keyword is a mirror of the already existing keyword DETACH, and it was introduced as part of Cypher®'s GQL conformance. Including it is functionally the same as using simple DELETE.

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

This also deletes the Person node Tom Hanks.

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

DETACH DELETE is not suitable for deleting large amounts of data, but is useful when experimenting with small example datasets. To delete large amounts of data, instead use CALL subqueries in transactions.