Archive
HowTo: Delete all nodes and relationships from Neo4j graph database
At a Neo4j question in http://stackoverflow.com/questions/19624414/delete-node-and-relationships-using-cypher-query-over-rest-api, a recent reply (older ones use obsolete Cypher syntax) says:
Both
START
and the[r?]
syntax are being phased out. It’s also usually not advised to directly use internal ids. Try something like:
match (n{some_field:"some_val"})
optional match (n)-[r]-()
delete n,r
So, to delete all nodes (including disconnected ones) and their relationships you could do:
MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r
(nice that it works in a single line too)
However since we delete ALL nodes and relationships, this one looks cleaner:
MATCH (n), ()-[r]-() DELETE n,r
Update #1
Michael Hunger kindly commented below on this last query:
In your last query you create a huge cross product.
All nodes times all relationships.Probably cleaner then to split it into two, delete rels first then nodes
Indeed, using PROFILE before the query it seems to do more work from a quick look (hoped it would be a bit more clever to optimize this, but maybe I’m asking too much). So probably should change it to two queries:
MATCH ()-[r]-() DELETE r
and
MATCH (n) DELETE n
Update #2
For deleting really big graphs, checkout an answer by Stefan Armbruster on how to delete in an iterative way at
…the most easy way is to stop Neo4j, drop the
data/graph.db
folder and restart it.Deleting a large graph via Cypher will be always slower but still doable if you use a proper transaction size to prevent memory issues (remember transaction are built up in memory first before they get committed). Typically 50-100k atomic operations is a good idea. You can add a limit to your deletion statement to control tx sizes and report back how many nodes have been deleted. Rerun this statement until a value of 0 is returned back:
MATCH (n) OPTIONAL MATCH (n)-[r]-() WITH n,r LIMIT 50000 DELETE n,r RETURN count(n) as deletedNodesCount