IamVeryClever
IamVeryClever

Reputation: 35

How to delete all nodes and all relationship in neo4j

I know a lot of similar questions have been asked. But it seems like the answers can't solve my problem. I have a very big graph, about 1 Million nodes and 3 million relations. When I try to delete them using following statements:

MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r

Then server is crashed. OK, I added LIMIT:

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

The server will not crash, but an exception shows up:

org.neo4j.kernel.api.exceptions.TransactionFailureException: Node record Node[1578965,used=false,rel=662269,prop=-1,labels=Inline(0x0:[]),light] still has relationships

I guess the LIMIT 1000000 will stop my deleting on relationship. Then it turns to delete the node. which cause that exception. Am I right? because I don't quite understand the process of LIMIT 1000000.

So what should I do? I don't want to drop the database file each time I need to clear my graph. Setting a larger memory space might help, but the size of my graph also might become larger. The crash will happen eventually.


Thanks for all your reply. I tried both your method. An exception "Neo.DatabaseError.Statement.ExecutionFailure" will show up in the second time I run the statement:

MATCH (n)
WITH n LIMIT 1000000
DETACH DELETE n

or this statement:

MATCH ()-[r]-()
WITH r
LIMIT 1000000
DELETE r

I give up. I think I'd better delete all files in my folder. That's a simple way for me.

Upvotes: 1

Views: 2163

Answers (3)

Ahmad Ghasemi
Ahmad Ghasemi

Reputation: 7

First delete all relationship with:

start r=relationship(*) delete r;

And Delete All Nodes:

MATCH (n) WITH n LIMIT 1000000 DETACH DELETE n.

Upvotes: 0

Luanne
Luanne

Reputation: 19373

On Neo4j 2.3 you can use

MATCH (n)
WITH n LIMIT 1000000
DETACH DELETE n

Otherwise, you can try limiting by nodes (only good if each individual node does not have a huge number of relations):

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

Upvotes: 5

MicTech
MicTech

Reputation: 45013

What you can do is delete relationships first and then nodes.

relationships

MATCH ()-[r]-()
WITH r
LIMIT 1000000
DELETE r

nodes

MATCH (n)
WITH n
LIMIT 1000000
DELETE n

Upvotes: 4

Related Questions