Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

neo4j

cypher

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.

like image 503
Aran Mulholland Avatar asked Oct 15 '12 20:10

Aran Mulholland


People also ask

How do I return all nodes and relationships in Neo4j?

When you want to return all nodes, relationships and paths found in a query, you can use the * symbol. This returns the two nodes, the relationship and the path used in the query.

Can relationships have properties in Neo4j?

The graphs in the Neo4j Graph Data Science Library support properties for relationships. We provide multiple operations to work with the stored relationship-properties in projected graphs. Relationship properties are either added during the graph projection or when using the mutate mode of our graph algorithms.

What is optional match in Neo4j?

An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.

What is graph Cypher?

Cypher is Neo4j's graph query language that lets you retrieve data from the graph. It is like SQL for graphs, and was inspired by SQL so it lets you focus on what data you want out of the graph (not how to go get it).


2 Answers

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; 
like image 83
Eve Freeman Avatar answered Oct 04 '22 03:10

Eve Freeman


Would this work for you?

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

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

like image 21
Jason Sperske Avatar answered Oct 04 '22 01:10

Jason Sperske