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
).
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
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
.
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.
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
|