47
votes

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.

3

3 Answers

95
votes

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;
14
votes

Would this work for you?

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

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

6
votes

This just works fine in 2.0:

    MATCH n RETURN n