Overview
The DELETE
statement allows you to delete nodes and edges. These nodes or edges must first be retrieved using the MATCH
statement.
<delete statement> ::= [ "DETACH" | "NODETACH"] "DELETE" <delete item list>
<delete item list> ::= <delete item> [ { "," <delete item> }... ]
<delete item> ::= <value expression>
An edge cannot exist when any of its endpoints is removed from the graph. By default, GQL does not allow to delete a node while it still has edges connected to it.
However, you can explicitly use the keyword DETACH
to enable the deletion of nodes along with their connected edges. For example, when node B
is deleted, edges 1
, 2
and 4
will also be deleted.
In the case of DELETE
or NODETACH DELETE
, the deletion of node B
will fail, which can be useful as a security measure to prevent unintended deletions.
If neither DETACH
nor NODETACH
is specified, DEDETACH
is implicitly applied.
Example Graph
Each of the following examples begins with this graph:
To create this graph, run the following query against an empty graph:
INSERT (rowlock:User {_id: "U01", name: "rowlock"}),
(brainy:User {_id: "U02", name: "Brainy"}),
(mochaeach:User {_id: "U03", name: "mochaeach"}),
(purplechalk:User {_id: "U04", name: "purplechalk"}),
(c:Club {_id: "C01"}),
(rowlock)-[:Follows]->(brainy),
(mochaeach)-[:Follows]->(brainy),
(brainy)-[:Joins]->(c)
Deleting Isolated Nodes
This query deletes the isolated node purplechalk
:
MATCH (n:User {name: 'purplechalk'})
DELETE n
Deleting Non-Isolated Nodes
This query deletes the node rowlock
along with one edge connected to it:
MATCH (n:User {name: 'rowlock'})
DETACH DELETE n
This query fails since the node rowlock
has an edge connected to it:
MATCH (n:User {name: 'rowlock'})
DELETE n
Deleting Edges
This query deletes all edges labeled Follows
:
MATCH ()-[e:Follows]->()
DELETE e