I'm trying to execute following query:
MATCH (movie:Movie {title:"test"})-[r]-() DELETE movie, r
to delete a :Movie node and all its relationships. It's all good, except if the query doesn't have any relationships, it fails to MATCH the movie. I've tried with OPTIONAL MATCH, but no luck.
I'm looking for a way to DELETE a movie node no matter if it has or doesn't have any relationships, but if it has, to DELETE them as well.
Deleting Nodes and Relationships Deleting all nodes and relationships in a Neo4j database is very simple. Here is an example that does just that: MATCH (n) DETACH DELETE n; The DETACH keyword specifies to remove or “detach” all relationships from a particular node before deletion.
Deleting relationship is as simple as deleting nodes. Use the MATCH statement to match the relationships you want to delete. You can delete one or many relationships or all relationships by using one statement.
The REMOVE clause is used to remove properties and labels from graph elements (Nodes or Relationships). DELETE operation is used to delete nodes and associated relationships. REMOVE operation is used to remove labels and properties.
In new Neo4j versions (since 2.3 I think) you can use such syntax:
MATCH (movie:Movie {title:"test"})
DETACH DELETE movie
There's OPTIONAL MATCH
:
MATCH (movie:Movie {title:"test"})
OPTIONAL MATCH (movie)-[r]-()
DELETE movie, r
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With