Delete a node and its relations (if any) in Neo4j

I am trying to execute the following query:

MATCH (movie:Movie {title:"test"})-[r]-() DELETE movie, r 

delete: the movie node and all its relationships. This is all good, except that the request does not have any relationship, it cannot match the movie. I tried with the OPTIONAL MATCH, but no luck.

I am looking for a way to DELETE a movie node, regardless of whether it has a relationship or not, but if it is, DELETE them.

+14
source share
2 answers

There OPTIONAL MATCH :

 MATCH (movie:Movie {title:"test"}) OPTIONAL MATCH (movie)-[r]-() DELETE movie, r 
+16
source

In newer versions of Neo4j (starting with 2.3, I think) you can use this syntax:

 MATCH (movie:Movie {title:"test"}) DETACH DELETE movie 
+17
source

Source: https://habr.com/ru/post/984677/


All Articles