3
votes

When attempting to delete all nodes from my Neo4j graph database, which I have successfully done many times in the past on smaller datasets, I consistently ran across Error: undefined - undefined after running this query

MATCH (n) DETACH DELETE n

I figured the number of nodes I was attempting to delete at once may be too large (>100000), so I then tried this query

MATCH (n) OPTIONAL MATCH (n)-[r]-() WITH n,r LIMIT 10000 DELETE n,r

and many variations of it, acting on mostly what I read in this post: Best way to delete all nodes and relationships in Cypher. All returned this error

org.neo4j.kernel.api.exceptions.ConstraintViolationTransactionFailureException: Cannot delete node<32769>, because it still has relationships. To delete this node, you must first delete its relationships.

and each time, the node Neo4j could not delete differs. Is there any way of resolving this issue?

Perhaps also noteworthy, while desperately running variations of the previous query, when I ran this query

MATCH ()-[r]-() OPTIONAL MATCH (n)-[r]-() WITH r,n LIMIT 10000 DELETE r,n

I got this rather unique error

Java heap space

in the console, which showed up as Neo.DatabaseError.General.UnknownError in the banner.

1

1 Answers

1
votes

One of the problems with the way your queries are written is that each DELETE is only attempting to delete a single node/relationship pair at a time. If any nodes have more than one relationship, you will get that constraint violation error, since in order to delete a node you have to delete all of its relationships (either beforehand or at the same time).

To delete 10000 nodes (and all their relationships), use this query:

MATCH (n)
WITH n LIMIT 10000
DETACH DELETE n;