I'm connecting to the server via bolt. I need to delete all nodes and relations then create a new graph.
Once I run
with driver.session() as session:
session.run("""CALL apoc.periodic.iterate('MATCH (n) RETURN n', 'DETACH DELETE n', {batchSize:10, parallel:false})""")
Or result = session.run("""CALL apoc.periodic.commit('MATCH(s) WITH s LIMIT $num DETACH DELETE s RETURN count(*) AS c', {num: 1000})""")
Or session.run("MATCH (n) DETACH DELETE n")
Then all nodes and relations are deleted, which is right. However, when I run the script again (after creating new nodes and relations), then the query is hung and I have to restart the server so the script is workin.
What is the problem here?
I'm using the latest version of neo4j and I'm facing this issue from different previous versions.
If you have a large number of nodes, MATCH (n) DETACH DELETE n won't work, because you may need to do a transaction that's bigger than the memory of your machine. This can cause out of memory errors.
The apoc.periodic.iterate approach is the right way to go because it splits the deletes into batches of manageable size (although batchSize: 10 is very, very small and could be bigger!)
Deleting the entire database simply takes some time, because Neo4j keeps transactional state about each change made to the database. If your goal is to entirely delete a database quickly, consider using DROP DATABASE foo as a command on the system database, which destroys everything much, much faster than transactionally deleting each individual node.
Thank you for your reply. I have tried apoc.periodic.iterate and still having the same issue. My nodes are very small.
I have a deleting lines at the beginning of my application and at the last I call a user-defined procedure (which uses the same db connection). What I notice is when I run the code at the first time, everything is OK. However, it hangs at the deleting line in the next run. So I have to restart the DB every time. I don't know how to deal with this problem!