cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Delete edges matched under consideration of edge properties

Assume a simplistic graph created by:

merge (n:Node {name: "x"})
merge (m:Node {name: "y"})
merge (n)-[:R]->(m)
merge (n)-[:R {k: "v"}]->(m)
return n, m

Naturally, I can delete both edges via:

match (n:Node {name: "x"})
match (m:Node {name: "y"})
match (n)-[r:R]->(m)
delete r
return n, m

and I have confirmed, that I can delete only the second edge via:

match (n:Node {name: "x"})
match (m:Node {name: "y"})
match (n)-[r:R {k: "v"}]->(m)
delete r
return n, m

However, I have a hard time deleting only the first edge. When I tried the following, both edges are deleted

match (n:Node {name: "x"})
match (m:Node {name: "y"})
match (n)-[r:R {}]->(m)
delete r
return n, m

What does work is

match (n:Node {name: "x"})
match (m:Node {name: "y"})
match (n)-[r:R]->(m)
where not exists(r.k)
delete r
return n, m

However, this is very inconvenient for me, because I need to know the that the other edge has a property k. I would prefer to match (and then delete) edges "without any property"

1 ACCEPTED SOLUTION

Hi,

I don't know if this code is recommended or not, but in this case it works.

MATCH (n:Node {name: 'x'})
MATCH (m:Node {name: 'y'})
MATCH (n)-[r:R]->(m)
  WHERE properties(r) IN [{}]
DELETE r
RETURN n, m

Koji

View solution in original post

2 REPLIES 2

Hi,

I don't know if this code is recommended or not, but in this case it works.

MATCH (n:Node {name: 'x'})
MATCH (m:Node {name: 'y'})
MATCH (n)-[r:R]->(m)
  WHERE properties(r) IN [{}]
DELETE r
RETURN n, m

Koji

Thanks a lot, it works indeed.

I have a follow-up question:
What would be a good way to add an edge without properties iff there is no edge without properties already?

Assuming my example graph after the delete statement you provided (so there already exists and edge, but that one has properties):

MERGE (n)-[:R {}]->(m) matches the remaining edge with properties and does not create anything. CREATE (n)-[:R {}]->(m) would also create multiple edges.

MATCH (n:Node {name: 'x'})
MATCH (m:Node {name: 'y'})
OPTIONAL MATCH (n)-[r:R]->(m)
   WHERE properties(r) IN [{}]
DELETE r
CREATE (n)-[:R {}]->(m)
RETURN n, m

seems to work well, but looks unnecessarily complicated. I also though this might turn multiple edges without annotations into a single one but a colleague pointed out it doesn't. Great but tbh it still slightly confuses me. I think what I would ideally like to have is a way to match edges without annotations that is compatible with MERGE. Until then, this seems to do the trick, though.