Aran Mulholland
Aran Mulholland

Reputation: 23935

Neo4j: Get all nodes in a graph, even those that are unconnected by relationships

Using Cypher how can I get all nodes in a graph? I am running some testing against the graph and I have some nodes without relationships so am having trouble crafting a query.

The reason I want to get them all is that I want to delete all the nodes in the graph at the start of every test.

Upvotes: 52

Views: 59762

Answers (4)

Dave Ranjan
Dave Ranjan

Reputation: 2984

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.

If your nodes have more than 100 relationships per node ((100+1)*10k=>1010k deletes) reduce the batch size or see the recommendations at the bottom.

With 4.4 and newer versions you can utilize the CALL {} IN TRANSACTIONS syntax.

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

With 3.x forward and using APOC

call apoc.periodic.iterate("MATCH (n:Foo) where n.foo='bar' return id(n) as id", "MATCH (n) WHERE id(n) = id DETACH DELETE n", {batchSize:10000})
yield batches, total return batches, total

For best practices around deleting huge data in neo4j, follow these guidelines.

Upvotes: 0

Eve Freeman
Eve Freeman

Reputation: 33145

So, this gives you all nodes:

MATCH (n)
RETURN n;

If you want to delete everything from a graph, you can do something like this:

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

Updated for 2.0+

Edit: Now in 2.3 they have DETACH DELETE, so you can do something like:

MATCH (n)
DETACH DELETE n;

Upvotes: 103

boggle
boggle

Reputation: 221

This just works fine in 2.0:

    MATCH n RETURN n

Upvotes: 7

Jason Sperske
Jason Sperske

Reputation: 30416

Would this work for you?

START a=node:index_name('*:*')

Assuming you have an index with these orphaned nodes in them.

Upvotes: 14

Related Questions