When deleting a large number of nodes (1000+), it's important to stagger the delete operations so as not to overwhelm the AuraDB Instance.
If you intend to wipe completely a database see How to wipe out / delete all the content in a Neo4j AuraDB Instance ?
If you need to be selective learning Neo4j's Cypher language you may have used a simple query (see below).
Do not use this statement for 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 proceed to load all the nodes and consume a large amount of memory.
SOLUTIONS
Non Cypher Based Solution (Professional Tier)
If you wanted to clear the database instance completely, rather than remover 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 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 proceed 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});
Using CALL {} IN TRANSACTIONS
Since 4.4 the best way to achieve that same efficiency is to make use of CALL {} IN TRANSACTIONS.
As per the nature of storing graph the best approach is to first delete the relationships and then to 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 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.
Comments
0 comments
Article is closed for comments.