my neo4j database data cannot be emptied by command : MATCH (n) DETACH DELETE n. I keeps getting disconnect in the middle of data purge.
matthew.wood@neo4j.com
If your database is very large and you are still experiencing memory issues when deleting in transactions as shown above, you could try manually batching that whole operation - something like this:
//Unwinding Batched Delete
:auto
// Step 1: Get the node count and use it to set the batch size
CALL {
MATCH (n)
RETURN COUNT(n) AS nodeCount
}
WITH nodeCount, 1000 AS batchSize
WITH nodeCount, batchSize, toInteger(ceil(toFloat(nodeCount) / batchSize)) AS numberOfBatches
// Generate the series for the UNWIND
UNWIND range(0, numberOfBatches - 1) AS i
CALL {
WITH i, numberOfBatches, batchSize
MATCH (n)
WHERE id(n) % numberOfBatches = i
WITH n LIMIT 1000
DELETE n
RETURN COUNT(n) AS count
}
// You could experiment with the number of concurrent transactions and number of rows below to achieve the best throughput
// Or just try with the default values ('IN CONCURRENT TRANSACTIONS')
IN 2 CONCURRENT TRANSACTIONS OF 50 ROWS
ON ERROR FAIL
MATCH (n) DELETE n
RETURN nodeCount, numberOfBatches, batchSize
matthew.wood@neo4j.com
A first step would be to try executing the DETACH DELETE operation in transactions, like this:
MATCH (n)
CALL (n) {
DETACH DELETE n
} IN TRANSACTIONS
Where you have multiple processors available, you can ou can also try that with CONCURRENT TRANSACTIONS:
MATCH (n)
CALL (n) {
DETACH DELETE n
} IN CONCURRENT TRANSACTIONS
(The level of concurrency and batch size is configurable - please see the docs here for more details: https://neo4j.com/docs/cypher-manual/current/subqueries/subqueries-in-transactions/