How to DELETE nodes or relationship with NULL properties in neo4j 2.0 with cypher
Asked Answered
C

3

9

I have created some nodes and relationships within these. But while doing these something wrong happened. I want to delete all nodes having "SGS", except node with ID 2.

Following is script I ran to create node and relationships:

(Please also suggest how to EDIT this one, if possible)

I tried DELETE (along with match, where, IS NULL, etc) with some different trial and error methods but unable to achieve same. Please Help.

Cockayne answered 6/1, 2014 at 11:55 Comment(2)
Hi, as this was my first question on SE, you might found it messy. I request you to bare me this time.Cockayne
Regarding your query, you have a '<br>' in there at line 15.Elna
M
8

It looks like all your nodes have a name, except the node that you want deleted. So you can try

match n
WHERE NOT (HAS (n.name)) //find all nodes with no name property
with n
match n-[r]-() //find all nodes connected to that node
delete r,n //delete its relations and then the node itself

(note not tested)

Looks like in your create statement above, you've use (rkn) instead of (rk) and also probably (answ) instead of something else. So (rkn)-[:READ]->(bk1) will just create a node with no properties to represent rkn and a READ relation to OneNight@CallCenter. You just have to verify and fix those.

e.g. the relationships all refer to a node called (rkn) so I renamed the rk node in your create statement to rkn. Please also verify that every node you refer to in your relationships actually maps to a node in your create statement.

NOTE: HAS is no longer supported in Cypher, please use EXISTS instead!

Mumble answered 6/1, 2014 at 12:35 Comment(3)
Thanks Luanne, It worked. I just started to work on this technology 3days ago, so was not aware about "WITH/HAS" etc. Is it possible to answer that how to edit the same(what I have mistaken while creation)?Cockayne
I tried with this early (as I am aware about use of UNIQUE) but got (and yet getting) same error:This pattern is not supported for CREATE UNIQUE.Cockayne
Your question about the PatternException is a different question than the one about deleting nodes, so you should ask it separately. If you do, someone might answer that CREATE UNIQUE requires some part of the pattern to be bound already–first match something, then create unique rels and nodes from that something, i.e. MATCH (a {name:"A"}) CREATE UNIQUE a-[:R]->(b {name:"B"}). If you want unique single nodes use MERGE.Insolent
C
4

This one worked for me

match (n) WHERE NOT (EXISTS (n.name)) DETACH DELETE n
Cascara answered 23/10, 2017 at 21:43 Comment(0)
C
1

None of the above will work currently in cypher.

Try below cypher query. This works:

//find all nodes with no name property.

MATCH (n) WHERE NOT ((n.name)IS NOT NULL)

//detach all its relationships and delete the found node i.e. empty node.

DETACH DELETE n

Copal answered 24/5, 2023 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.