Neo4 / Cypher: remove all node relationships

I would like to remove all relationships (inbound and outbound) from a node (relationships can go on different nodes)

something like:

start n = node :( 1000) MATCH n <- [r] → anynode DELETE r

Doesn't work because here "anynode" is interpreted as the "first" linked node that matches.

Any ideas?

+3


source to share


2 answers


Do you want to:



start n=node(1000) 
match n-[r]-() 
delete r;

      

+13


source


I am having a problem using @ eve-freeman's solution when trying to delete all relationships as part of a larger update action (i.e. delete existing relationships and then create new ones and also update the node property):

MATCH (n:Node { uuid: '1' })
OPTIONAL MATCH (n)-[r]-()
DELETE r
WITH n
MATCH (f:Foo { uuid: '2' })
CREATE (n)-[:LIKES]->(f)
WITH n
MATCH (b:Bar { uuid: '3' })
CREATE (n)<-[:LOVES]-(b)
SET n.name = 'Howard'
RETURN n

      

For some reason, it created a duplicate entry (I would be interested to know why this is happening):

"n"
{"name":"Howard","uuid":"1"}
{"name":"Howard","uuid":"1"}
Set 2 properties, deleted 2 relationships, created 4 relationships, started streaming 2 records after 6 ms and completed after 6 ms.

      



Using below ( COLLECT

and then FOREACH

) works:

MATCH (n:Node { uuid: '1' })
OPTIONAL MATCH (n)-[r]-()
WITH n, COLLECT (r) AS rels
FOREACH (r IN rels | DELETE r)
WITH n
MATCH (f:Foo { uuid: '2' })
CREATE (n)-[:LIKES]->(f)
WITH n
MATCH (b:Bar { uuid: '3' })
CREATE (n)<-[:LOVES]-(b)
SET prd.name = 'Howard'
RETURN n

      

Return:

"n"
{"name":"Howard","uuid":"1"}
Set 1 property, deleted 2 relationships, created 2 relationships, started streaming 1 record after 3 ms and completed after 3 ms.

      

0


source







All Articles