3.12. DELETE

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

3.12.1. Introduction

For removing properties and labels, see Section 3.14, “REMOVE”. Remember that you cannot delete a node without also deleting relationships that start or end on said node. Either explicitly delete the relationships, or use DETACH DELETE.

The examples start out with the following database:

Figure 3.11. Graph
alt

3.12.2. Delete single node

To delete a node, use the DELETE clause.

Query. 

MATCH (n:Person { name: 'UNKNOWN' })
DELETE n

Table 3.89. Result

0 rows Nodes deleted: 1

(empty result)

Try this query live.  CREATE (a:Person {name: 'Andy', age: 36}), (p:Person {name: 'Timothy', age: 25}), (t:Person {name: 'Peter', age: 34}), (z:Person {name: 'UNKNOWN'}), (a)-[:KNOWS]->(t), (a)-[:KNOWS]->(p) MATCH (n:Person {name: 'UNKNOWN'}) DELETE n

3.12.3. Delete all nodes and relationships

This query isn’t for deleting large amounts of data, but is useful when experimenting with small example data sets.

Query. 

MATCH (n)
DETACH DELETE n

Table 3.90. Result

0 rows Nodes deleted: 4 Relationships deleted: 2

(empty result)

Try this query live.  CREATE (a:Person {name: 'Andy', age: 36}), (p:Person {name: 'Timothy', age: 25}), (t:Person {name: 'Peter', age: 34}), (z:Person {name: 'UNKNOWN'}), (a)-[:KNOWS]->(t), (a)-[:KNOWS]->(p) MATCH (n) DETACH DELETE n

3.12.4. Delete a node with all its relationships

When you want to delete a node and any relationship going to or from it, use DETACH DELETE.

Query. 

MATCH (n { name: 'Andy' })
DETACH DELETE n

Table 3.91. Result

0 rows Nodes deleted: 1 Relationships deleted: 2

(empty result)

Try this query live.  CREATE (a:Person {name: 'Andy', age: 36}), (p:Person {name: 'Timothy', age: 25}), (t:Person {name: 'Peter', age: 34}), (z:Person {name: 'UNKNOWN'}), (a)-[:KNOWS]->(t), (a)-[:KNOWS]->(p) MATCH (n {name: 'Andy'}) DETACH DELETE n

3.12.5. Delete relationships only

It is also possible to delete relationships only, leaving the node(s) otherwise unaffected.

Query. 

MATCH (n { name: 'Andy' })-[r:KNOWS]->()
DELETE r

This deletes all outgoing KNOWS relationships from the node with the name 'Andy'.

Table 3.92. Result

0 rows Relationships deleted: 2

(empty result)

Try this query live.  CREATE (a:Person {name: 'Andy', age: 36}), (p:Person {name: 'Timothy', age: 25}), (t:Person {name: 'Peter', age: 34}), (z:Person {name: 'UNKNOWN'}), (a)-[:KNOWS]->(t), (a)-[:KNOWS]->(p) MATCH (n {name: 'Andy'})-[r:KNOWS]->() DELETE r