1

For example, I want to delete Actor node with id = "005A" and its connected Movie nodes. The relationship between Actor and Movie node is ACTED_IN.

I have tried this cypher query:

MATCH (a:Actor {id: "005A"})
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
DELETE a, r, m;

but it didn't work, I got TransactionFailureException: Unable to commit transaction error.

Anyone can give a solution?

UPDATE:

I found out that there is relationship from other node (Agency) to Actor node which labeled as OWNED. So the diagram is like this:

(aa:Agency)-[o:OWNED]->(a:Actor)
null
  • 8,669
  • 16
  • 68
  • 98

2 Answers2

3

[EDITED]

You cannot delete a node unless all of its relationships have also been deleted. In your case, it is possible that the a and/or m nodes have relationships other than r.

To get the set of relationship types associated with a and m, you can use the following (I've limited the result to 10 rows, in case a and/or m have a lot of relationships`):

MATCH (a:Actor {id: "005A"})
OPTIONAL MATCH ()-[rx]-(a)
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
OPTIONAL MATCH (m)-[ry]-()
RETURN a, r, m, COLLECT(DISTINCT TYPE(rx)), COLLECT(DISTINCT TYPE(ry))
LIMIT 10;

I suspect that your results will show types other than ACTED_IN.

The following query should delete the actor and all movies s/he acted in:

MATCH (a:Actor {id: "005A"})
OPTIONAL MATCH ()-[rx]-(a)
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
OPTIONAL MATCH (m)-[ry]-()
DELETE rx, ry, r, a, m;
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • I run `MATCH (m:Movie)-[r]->() return r;`, it returned empty row. – null Jan 13 '15 at 19:46
  • With your query, the `r` and `m` returned , the next two collects returned empty list (`[]`) – null Jan 13 '15 at 19:56
  • You said all of relationships must be deleted, what if the `Actor` node has **relationship from other node**? For example: `(aa:Agency)-[:OWNED]->(a:Actor)`. Should I delete the `OWNED` relationship too? – null Jan 13 '15 at 19:58
  • Yes. I have updated my query -- it should not return results for `COLLECT(DISTINCT TYPE(rx))` in that case. – cybersam Jan 13 '15 at 20:04
  • Ok, it seems I was right. I need to delete the `OWNED` relationship too. I will edit my question to give chance to you all to answer my question :) – null Jan 13 '15 at 20:05
  • See my updated query. It should now return results for `COLLECT(DISTINCT TYPE(rx))` in that case. And I now also answer your question. – cybersam Jan 13 '15 at 20:09
  • There is problem with your query, it only delete the `Actor` node and its relationship, but it doesn't delete the connected `Movie` nodes. Also, I have a question, what is the meaning of relationship line that doesn't have arrowhead like this: `(x)-[r]-()` ? – null Jan 13 '15 at 20:24
  • Have you actually tried my query? It should delete all movies the actor acted in. Arrowlessness means that the directionality does not matter. – cybersam Jan 13 '15 at 20:31
  • When I write that line, I didn't see your answer is being edited. Ok, it works now. Great answer :) – null Jan 13 '15 at 20:33
  • I have one more question, is the order of variables in `DELETE rx, ry, r, a, m` important or have influence in the query? like I should put the relationship variables first before nodes? – null Jan 13 '15 at 20:34
  • The order does not matter. – cybersam Jan 13 '15 at 20:36
0

Are you sure the id is encoded as a string property ? I guess the id of the movie database is an integer and thus the id should not be encapsulated in quotes :

MATCH (a:Actor {id: 5})
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
DELETE a, r, m;
Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36
  • nope, it's really string and the value is unique. To prevent anyone think like this in future, I will change the example a bit. – null Jan 13 '15 at 19:21