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.

Delete query hangs until restart the server

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.

4 REPLIES 4

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!

Did you get any solution ? Facing the same problem.

@urvis @Amal 

Can you try using the https://neo4j.com/labs/apoc/4.1/overview/apoc.periodic/apoc.periodic.truncate/ ?

This procedure is precisely for this case,

it just execute these 2 apoc.periodic.iterate:

CALL apoc.periodic.iterate("MATCH ()-[r]->() RETURN id(r) as id", "MATCH ()-[r]->() WHERE id(r) = id DELETE r", {config..})

 and 

CALL apoc.periodic.iterate("MATCH (n) RETURN id(n) as id", "MATCH (n) WHERE id(n) = id DELETE n", {config..})