Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j: How do I delete a specific relationship with cypher?

Tags:

neo4j

cypher

Lets say, I have an user:

CREATE (n { name: 'Tamil' })

and 2 roles:

CREATE (n { name: 'developer' } ) 
CREATE (n { name: 'tester' } )

Then, I make relationship between the user & each of the 2 roles.

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'developer' 
CREATE (a)-[r:HAS_ROLE]->(b) 
RETURN r

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'tester' 
CREATE (a)-[r:HAS_ROLE]->(b) 
RETURN r

Now, I want to remove tester role relationship from the user. I tried:

CYPHER 1.9  START a = node:node_auto_index('name:Tamil') 
MATCH a-[r:HAS_ROLE]-() 
RETURN r

But, it returns both of the relationships. I know that i can attach property with relationships. But, again, I don't know the cypher syntax for that.

I am new to Neo4j. Any suggestions would be really great!

Thanks!


2 Answers

I deleted the relationship on your original graph with this query:

START n=node(*) 
MATCH (n)-[rel:HAS_ROLE]->(r) 
WHERE n.name='Tamil' AND r.name='tester' 
DELETE rel
like image 52
Werner Kvalem Vesterås Avatar answered Sep 14 '25 20:09

Werner Kvalem Vesterås


I found it. I changed the relationships to have property. Like this:

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'developer' 
CREATE (a)-[r:HAS_ROLE {id: xyz}]->(b) 
RETURN r

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'tester' 
CREATE (a)-[r:HAS_ROLE {id: abc}]->(b) 
RETURN r

Then this below code deleted the specified relationship.

CYPHER 1.9  START a = node:node_auto_index('name:Tamil') 
MATCH a-[r:HAS_ROLE]-() 
WHERE r.id = abc
DELETE r;

I am not sure that this is the right way to do or not. But, it works.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!