12.4. Delete
The DELETE
clause is used to delete graph elements — nodes and relationships.
For removing properties and labels, see Section 12.5, “Remove”.
The examples start out with the following database:
Delete single node
To delete a node, use the DELETE
clause.
Query
Nothing is returned from this query, except the count of affected nodes.
Result
Nodes deleted: 1 |
---|
|
Try this query live create (_0 {`age`:25, `name`:"Tobias"}) create (_1 {`age`:34, `name`:"Peter"}) create (_2 {`age`:36, `name`:"Andres"}) create (_3:`Useless`) create _2-[:`KNOWS`]->_1 create _2-[:`KNOWS`]->_0 match (n:Useless) delete n
Delete a node and connected relationships
If you are trying to delete a node with relationships on it, you have to delete these as well.
Query
Nothing is returned from this query, except the count of affected nodes.
Result
Nodes deleted: 1 |
---|
Relationships deleted: 2 |
|
Try this query live create (_0 {`age`:25, `name`:"Tobias"}) create (_1 {`age`:34, `name`:"Peter"}) create (_2 {`age`:36, `name`:"Andres"}) create _2-[:`KNOWS`]->_1 create _2-[:`KNOWS`]->_0 match (n {name: 'Andres'})-[r]-() delete n, r
Delete all nodes and relationships
This query isn’t for deleting large amounts of data, but is nice when playing around with small example data sets.
Query
Nothing is returned from this query, except the count of affected nodes.
Result
Nodes deleted: 3 |
---|
Relationships deleted: 2 |
|
Try this query live create (_0 {`age`:25, `name`:"Tobias"}) create (_1 {`age`:34, `name`:"Peter"}) create (_2 {`age`:36, `name`:"Andres"}) create _2-[:`KNOWS`]->_1 create _2-[:`KNOWS`]->_0 MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r