Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j delete all nodes with a given label and their relationships in one query

Tags:

neo4j

I separate my Neo4j database into isolated sub-databases using labels. During development, I frequently need to wipe an entire sub-database clean. Currently I do this with:

MATCH (n:myLabel)-[r]-() DELETE n, r
MATCH (n:myLabel) DELETE n

I need two queries because I have to delete all relationships, at the same time as their nodes, but I don't know how to match unconnected nodes simultaneously. Is there a way to wipe out a whole subgraph marked by a label in a single query? I'm on Neo4j 2.2.1

like image 671
Sean Mackesey Avatar asked Apr 30 '15 13:04

Sean Mackesey


1 Answers

Here you go:

MATCH (n:myLabel) OPTIONAL MATCH (n)-[r]-() DELETE n, r
like image 61
Brian Underwood Avatar answered Oct 11 '22 06:10

Brian Underwood