Software Dev
Software Dev

Reputation: 1102

Neo4j Delete All Nodes and Relationships

I want to delete all nodes and relationships.

I run MATCH ()-[r]-() DELETE r to delete all the relationships.

Then, I run MATCH (n) DELETE n to delete all the nodes. It does delete all the nodes, but the problem is that it also gives me this error:

Neo.DatabaseError.Transaction.TransactionCommitFailed
Unable to complete transaction.

How do I delete all nodes and relationships with getting this error?

Upvotes: 0

Views: 2120

Answers (2)

Thennan
Thennan

Reputation: 898

It looks like your database is hitting an OOM Exception.
As stated in Large Delete Transaction Best Practices in Neo4j

If you need to delete some large number of objects from the graph, one needs to be mindful of the not building up such a large single transaction such that a Java OUT OF HEAP Error will be encountered.
  1. Delete all constraints and indexes

    CALL apoc.schema.assert({},{},true);

  2. Batch delete with CALL {} IN TRANSACTIONS syntax or apoc.periodic.iterate:

MATCH (n:Foo) where n.foo='bar'
CALL { WITH n
DETACH DELETE n
} IN TRANSACTIONS OF 10000 ROWS;

OR

CALL apoc.periodic.iterate(
"MATCH (n) RETURN n",
"DETACH DELETE n",
{batchSize:10000, parallel:false})

Upvotes: 0

jose_bacoy
jose_bacoy

Reputation: 12684

To delete all nodes and all relationships, I do DETACH DELETE

Reference: https://neo4j.com/docs/cypher-manual/current/clauses/delete/#delete-delete-all-nodes-and-relationships

 MATCH (n)
 DETACH DELETE n;

If nothing works, then rename (or remove) your neo4j data folder and restart your server.

<HOME_NEO4j>/data/data/transactions/neo4j

Upvotes: 1

Related Questions