When deleting a large number of nodes (1000+), it's important to stagger the delete operations not to overwhelm the AuraDB Instance.
If you intend to wipe a database completely, see How to wipe out / delete all the content in a Neo4j AuraDB Instance ?
You may have used a simple query (see below).
Do not use this statement for a large data set as this is not suited for large data set, as mentioned in the documentation.
MATCH (n) DETACH DELETE(n)
The main issue of the above command is that it first loads all the nodes and consumes a large amount of memory.
SOLUTIONS
Non-Cypher-Based Solution (Professional Tier)
If you wanted to clear the database instance completely rather than remove a number of nodes. You can use the "Reset to blank" option available in the Neo4j Aura console. Please refer to How to wipe out / delete all the content in a Neo4j AuraDB Instance for details on how to do this.
Cypher Based Solutions
Using CALL {} IN TRANSACTIONS
Since 4.4 the best way to achieve that same efficiency is to make use of CALL {} IN TRANSACTIONS.
Because of the way graphs are stored, the best approach is to delete the relationships first and then delete the nodes in separate operations.
// Delete all relationships
MATCH ()-[r]->()
CALL { WITH r
DELETE r
} IN TRANSACTIONS OF 50000 ROWS;
// Delete all nodes
MATCH (n)
CALL { WITH n
DETACH DELETE n
} IN TRANSACTIONS OF 50000 ROWS;
NOTE: if running from the Neo4j Browser, you will need to prefix the 2 Cypher statements with :auto
or you will see this error:
A query with 'CALL { ... } IN TRANSACTIONS' can only be executed in an implicit transaction, but tried to execute in an explicit transaction.
Using APOC.PERIODIC.ITERATE with a list of IDs (nodes and/or relationships)
Aura has some safeguards for its query memory, and from versions greater than 4.x you should use this improved method instead. If you have dense nodes, it may be more efficient to delete separately, starting with the relationships first and then proceeding with the nodes to avoid any contention/locking;
// Delete all relationships
CALL apoc.periodic.iterate(
'MATCH ()-[r]->() RETURN id(r) AS id',
'MATCH ()-[r]->() WHERE id(r)=id DELETE r',
{batchSize: 5000});
// Delete all nodes
CALL apoc.periodic.iterate(
'MATCH (n) RETURN id(n) AS id',
'MATCH (n) WHERE id(n)=id DELETE n',
{batchSize: 5000});
Comments
0 comments
Article is closed for comments.