Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-08-2022 05:59 PM
I usually use:
match (n) detach delete n
Even for a small graph, this usually takes a long time. Is there a better way to delete the graph using cypher?
Solved! Go to Solution.
12-11-2022 03:43 PM
Actuall, this worked:
return session.run('CALL apoc.periodic.iterate("MATCH (n) RETURN n","DETACH DELETE n", {batchSize:1000, parallel:false})')
12-09-2022 04:14 AM
what version of Neo4j?
have you tried what is suggested per https://neo4j.com/developer/kb/large-delete-transaction-best-practices-in-neo4j/ ?
12-09-2022 09:39 AM
@dana_canzano I am using Neo4j Community 4.4.12. I am uisng the Python driver to access the graph, and the code is:
with self.neo4j_driver.session() as session: session.write_transaction(lambda tx: tx.run('MATCH (n) CALL { WITH n DETACH DELETE n } IN TRANSACTIONS OF 10000 ROWS;'))
The error message:
neo4j.exceptions.DatabaseError: {code: Neo.DatabaseError.Statement.ExecutionFailed} {message: A query with 'CALL { ... } IN TRANSACTIONS' can only be executed in an implicit transaction, but tried to execute in an explicit transaction.}
Why is that?
12-09-2022 09:52 AM
12-09-2022 03:20 PM
Originally, I used "Match (n) DETACH DELETE n", which is a performance issue. But now per documentation suggestion, I used:
with self.neo4j_driver.session() as session: session.write_transaction(lambda tx: tx.run('MATCH (n) CALL { WITH n DETACH DELETE n } IN TRANSACTIONS OF 10000 ROWS;'))
This caused an error.
Also, if I execute 'Match (n) DETACH DELETE n' in the browser, it is a lot quicker than through Python Driver as below:
with self.neo4j_driver.session() as session:
session.write_transaction(lambda tx: tx.run('MATCH (n) DETACH DELETE n'))
12-10-2022 04:57 AM
The ‘call{} in transactions’ clause can only be used in an auto-commit transaction, and the cypher needs to begin with ‘:auto’.
https://neo4j.com/docs/java-manual/current/session-api/
the browser uses auto-commit transactions. Also, ‘session.run()’ uses an auto-commit transaction.
man alternative to call subquery in transactions is apoc.periodic.iterate
https://neo4j.com/labs/apoc/4.1/overview/apoc.periodic/apoc.periodic.iterate/
12-11-2022 03:11 PM
Can you give an example of how to use :auto or apoc.periodic.iterate() to delete the whole graph? (Match(n) DETACH DELETE N)
12-11-2022 03:43 PM
Actuall, this worked:
return session.run('CALL apoc.periodic.iterate("MATCH (n) RETURN n","DETACH DELETE n", {batchSize:1000, parallel:false})')
12-11-2022 03:48 PM
That is an alternative to call in transactions. That should also work in a transaction function, which would be preferred.
12-09-2022 09:35 AM
Test
All the sessions of the conference are now available online