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.

Deleting a relationship IF a condition is true?

Just discovered Graph and Neo4j a couple of days ago and I must say I really like what it has to offer. I'm still tinkering around still, and now I wonder how I would proceed with conditions inside queries.
In the below code, how would I delete the relationship in the same query if the quantity is set to 0?

I have a hard time understanding the syntax of the conditionals inside Cypher.

    result = session.run(
        f'MATCH (order:Order {{ ref: "SomeOrder" }})<-[rel:IN_ORDER]-(product:Product {{ name: "SomeProduct" }}) '
        f'SET rel += {{ quantity: 0 }} '
        f'WITH order '
        f'MATCH (order)<-[r:IN_ORDER]-(p:Product) '
        f'RETURN order, p.name, p.price * r.quantity AS total_unit_price '
    )
3 REPLIES 3

MuddyBootsCode
Graph Steward

Hello! Welcome to the community. If that only thing your checking is the quantity on the relationship you can just do:

match(o:Order)<-[r:IN_ORDER]-(p:Product)
where r.quantity = 0
delete r
return o, p

That will check to see that the quantity is zero and if so delete the relationship.

Hello @andre.krosby1992 and welcome

If there are a lot of relations to delete, the best way to delete is:

CALL apoc.periodic.iterate('MATCH (o:Order)<-[r:IN_ORDER]-(p:Product)
WHERE r.quantity = 0 RETURN r', 'DELETE r', {batchSize:1000})

Regards,
Cobra

Also you can reference this article if you're looking for more info on conditional Cypher execution: