Skip to content

Instantly share code, notes, and snippets.

@tirthb
Created April 11, 2013 18:52
Show Gist options
  • Save tirthb/5366126 to your computer and use it in GitHub Desktop.
Save tirthb/5366126 to your computer and use it in GitHub Desktop.
Neo4j cypher query to remove orphan nodes i.e. nodes with no relationships
START n=node(*)
WHERE not(n-[*..2]-())
DELETE n;
@neutmute
Copy link

Here is a version where you can exclude some labels that are allowed to be orphans

MATCH   (n)
WHERE   (NOT (n)-[]-()) 
AND     (NOT ("AppSetting" in labels(n))) 
AND     (NOT ("User" in labels(n)))
RETURN n; 
// DELETE  n;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment