How can I get shortest path after delete some relationships?

I would like to delete some relationships in a graph first, and then find all pair shortest path.
Step1: MATCH (n {happy: false})-[r]-() DELETE r
Step2: MATCH p=allShortestPaths((h)-[*1..20]->(k {value:true}))

How can I combine these two steps in one query?

I don't think you can. It looks like DELETE is a terminal operation. I tried the query below to test. Although it executed without an error and deleted the relationship, it did not return the nodes n and m, even though a result is obtained if the second match/return is executed by itself. I suspect nothing is returned from the delete, so the query stops after the delete.

match(n{key:3})-[r:CONNECTED_TO]->(m{key:4}) delete r
with n
return n, m

Hello @lingxuan9 :slight_smile:

There is a workaround using the UNION keyword but this is an abuse and there is no guarantee that it will work in the future:

MATCH (n {happy: false})-[r]-() DELETE r
MATCH p=allShortestPaths((h)-[*1..20]->(k {value:true}))

The best option is to stick to two queries.


Thanks Cobra, I try it. But it shows
"All sub queries in an UNION must have the same column names"

So it was patched :slight_smile: It was working on previous versions.

Can you try:

MATCH (n {happy: false})-[r]-() DELETE r
MATCH p = allShortestPaths((h)-[*1..20]->(k {value:true}))