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

Aran Mulholland picture Aran Mulholland · Oct 15, 2012 · Viewed 49.8k times · Source

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.

Answer

Eve Freeman picture Eve Freeman · Oct 16, 2012

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;