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
).
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:
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.
Deleted 1 node
Delete relationships only
It is possible to delete a relationship while leaving the node(s) connected to that relationship otherwise unaffected.
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.
Deleted 1 relationship
Delete a node with all its relationships
To delete nodes and any relationships connected them, use the DETACH DELETE
clause.
MATCH (n:Person {name: 'Carrie-Anne Moss'})
DETACH DELETE n
This deletes the Person
node Carrie-Anne Moss
and all relationships connected to it.
Deleted 1 node, deleted 1 relationship
The |
Delete all nodes and relationships
It is possible to delete all nodes and relationships in a graph.
MATCH (n)
DETACH DELETE n
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. |
Was this page helpful?