Reputation: 1125
I'm trying to deletes 2 related nodes with all of their relationship.
This is the query that I've been trying to do that:
match (picture:Picture) where picture.id='101531146106623881437036830'
optional match ()-[r]- picture-[:UPLOADED_TO]-(facebook:Facebook)-[rf]-()
with r,rf,picture,facebook,picture.id as id
delete r,rf,picture,facebook
return id
Can't seem to get it right for some reason.
Upvotes: 2
Views: 57
Reputation: 8731
Your query is not working because you can't delete picture, a relation is still linked to it.
match (picture:Picture) where picture.id='101531146106623881437036830'
Ok you match the picture node, everything is fine here
optional match ()-[r]- picture-[:UPLOADED_TO]-(facebook:Facebook)-[rf]-()
Using optional match is a choice, but the problem here is that you didn't set a variable for UPLOADED_TO
, so this relation is seen by the query, but no references are made.
with r,rf,picture,facebook,picture.id as id
Here you push your references to the delete part (it's not needed in fact, only usefull pour aliases here)
delete r,rf,picture,facebook
return id
And finally you delete what you wanted to delete, but here you get an error "Can't delete node because it has relationships" or something like that. It's because UPLOADED_TO
relation still exists.
Simply add a reference to UPLOADED_TO
relation to delete it.
match (picture:Picture) where picture.id='101531146106623881437036830'
optional match ()-[r]- picture-[r2:UPLOADED_TO]-(facebook:Facebook)-[rf]-()
with r2,r,rf,picture,facebook,picture.id as id
delete r2,r,rf,picture,facebook
return id
MicTech's answer should work too, but I wanted to clarify your mistake to avoid doing it again ;)
Upvotes: 2
Reputation: 45003
What about this
MATCH ()-[r]-(p:Picture {id:'101531146106623881437036830'})-[:UPLOADED_TO]-(fb:Facebook)-[rf]-()
DELETE r, p, fb, r
Upvotes: 2