DELETE statement¶
The DELETE statement removes one or more elements from a graph or temporary graph.
Notes¶
When you delete graph elements using the DELETE statement, the system employs a logical deletion mechanism. Data is not immediately removed from the Storage Service but is instead marked as "deleted". This marked data is automatically excluded from query results and is not returned to users. The data is permanently removed during the background RocksDB Compaction process, which reclaims disk space.
Note
An operation involving a non-default schema or an object in it requires access to the schema, either implicitly or through an explicit USAGE grant, as well as the privilege required for the operation. The schema access check and the privilege check for the requested operation are independent; satisfying one does not satisfy the other. Schema access control does not apply to temporary graphs or built-in procedures and functions. For more information, see Schema access control.
Prerequisites¶
- A graph or temporary graph is created. For more information, see CREATE GRAPH and CREATE TEMPORARY GRAPH.
- To delete elements in a graph, you must be granted to perform the action
DELETE ANY GRAPHon the used schema or the actionDELETEon the graph. For more information, see Roles and privileges.
- To delete elements in a temporary graph, you must be the user with the role
ADMINor the owner of the temporary graph.
Syntax¶
- delete_statement
delete_statement ::=
( 'DETACH' | 'NODETACH' )? 'DELETE' value_expression ( ',' value_expression )*
Parameters¶
DETACH | NODETACH¶
Specifies whether to delete the nodes and their connecting edges or delete only the nodes.
DETACH: Deletes the nodes and their connecting edges.NODETACH: Deletes only the nodes. When deleting a node, an error is reported if the node has connecting edges. If neitherDETACHnorNODETACHis specified,NODETACHis used by default.
value_expression¶
A value expression that references the graph element (nodes or/and edges) to be deleted. For more information, see Value expressions.
Examples¶
The following examples are based on the example dataset.
Delete a node¶
'''
// Insert a node.
USE movie INSERT (a@Actor{id:10000, name: "Tom Hanks", birthDate: date("1956-07-09")})
'''
'''
// Delete the node.
USE movie MATCH (a@Actor{id:10000}) DELETE a
'''
Delete an edge¶
'''
USE movie
MATCH (a@Actor{name:"Mila Parély"})-[e@Act]->(b@Movie{name:"Beauty and the Beast"})
DELETE e
'''
The preceding statement deletes the edge -[e@Act]-> connecting the nodes a and b.
Delete a node and an edge¶
'''
USE movie
MATCH (a@Actor{name:"Nane Germon"})-[e@Act]->(b@Movie{name:"Beauty and the Beast"})
DELETE a, e
'''
The preceding statement deletes the node (a@Actor{name:"Nane Germon"}) and the edge -[e@Act]-> connecting the nodes a and b.
Use DETACH DELETE to delete a node and all the connecting edges¶
'''
USE movie
MATCH (a@Actor{name:"Josette Day"})
DETACH DELETE a
'''
The preceding statement deletes the node (a@Actor{name:"Josette Day"}) and all the connecting edges.
Delete all nodes and edges¶
USE movie MATCH (a) DETACH DELETE a
The preceding statement deletes all the nodes and edges in the movie graph.
Batch delete nodes with a table variable¶
'''
TABLE t { id } =
( 10001 ),
( 10002 ),
( 10003 )
USE movie
FOR re IN t
MATCH (a@Actor) WHERE a.id = re.id
DETACH DELETE a
'''
The preceding statement defines a table variable t and matches nodes of the node type Actor based on the field id in the table variable. It then deletes the matched nodes and all their connecting edges.
For more information about batch operations by using table variables, see Define table variables.