cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

What's the best cypher command to delete the whole graph?

lingvisa
Graph Fellow

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? 

1 ACCEPTED SOLUTION

Actuall, this worked:

 

return session.run('CALL apoc.periodic.iterate("MATCH (n) RETURN n","DETACH DELETE n", {batchSize:1000, parallel:false})')

View solution in original post

9 REPLIES 9

@dana_canzano 

@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? 

 

@lingvisa 

i thought this was a performance issue?  no ?   or a different issue ?

 

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'))

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/

Can you give an example of how to use :auto or apoc.periodic.iterate() to delete the whole graph? (Match(n) DETACH DELETE N)

Actuall, this worked:

 

return session.run('CALL apoc.periodic.iterate("MATCH (n) RETURN n","DETACH DELETE n", {batchSize:1000, parallel:false})')

That is an alternative to call in transactions. That should also work in a transaction function, which would be preferred. 

lingvisa
Graph Fellow

Test