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
The DELETE
statement can only delete isolated nodes, if any node specified has connected edges, an error will be thrown, and no nodes will be deleted.
Deleting Any Nodes
This query deletes the node rowlock
along with its connected edges:
MATCH (n:User {name: 'rowlock'})
DETACH DELETE n
This query deletes all nodes along with all edges:
MATCH (n)
DETACH DELETE n
Deleting Edges
This query deletes all edges labeled Follows
:
MATCH ()-[e:Follows]->()
DELETE e
Limiting the Amount to Delete
To limit the number of nodes or edges to delete, apply the LIMIT
statement after MATCH
to keep only the first N records before passing the variable to the DELETE
statement.
To delete any two edges:
MATCH ()-[e]->() LIMIT 2
DELETE e
RETURN e
Result: e
_uuid |
_from |
_to |
_from_uuid | _to_uuid | schema |
values |
---|---|---|---|---|---|---|
Sys-gen | U02 | C01 | UUID of U02 | UUID of C01 | Joins | |
Sys-gen | U01 | U02 | UUID of U01 | UUID of U02 | Follows |