cancel
Showing results for 
Search instead for 
Did you mean: 

How to "bulk" delete all relationships between nodes for a specific Node label?

kasipasi
Node Clone

Hello,

I want to remove/delete all the "parent" relationship between all node for Node label Traceparts.

What do I need to add to below code to remove all the relationships?

// Find all nodes with node label Traceparts
MATCH (n:Traceparts)
RETURN n

kasipasi_0-1664840046103.png

 

1 ACCEPTED SOLUTION

MATCH (n:Traceparts)-[r:parent]-(m:Traceparts)
WHERE id(n) < id(m)
DELETE r

You just need to add the relationship type to the relationship. I also added Tracepart label to the matching 'm' node in case you only want to delete relationships between these two node types. Remove it if you don't want that additional constraint. 

FYI- It is generally best practice to define relationship types in all capitals, such as PARENT. I also like to write them as a verb, such as HAS_PARENT. 

View solution in original post

3 REPLIES 3

glilienfield
Ninja
Ninja

Try this, if you want to delete all relationships attached to a node with Traceparts label. 

MATCH (n:Traceparts)-[r]-(m)
WHERE id(n) < id(m)
DELETE r

There is no constraint on this query other than label Tracepart. You should test it on one node first to ensure it is what you want. You can do this by added to the where clause to select just one node 'n'.  

Hi @glilienfield 

 

Great thank you! If I would like to add one more constraint to query, to only delete relationships called "parent" for node label Traceparts. What would I need to add then?

 

Thank you very much!

 

Isak

MATCH (n:Traceparts)-[r:parent]-(m:Traceparts)
WHERE id(n) < id(m)
DELETE r

You just need to add the relationship type to the relationship. I also added Tracepart label to the matching 'm' node in case you only want to delete relationships between these two node types. Remove it if you don't want that additional constraint. 

FYI- It is generally best practice to define relationship types in all capitals, such as PARENT. I also like to write them as a verb, such as HAS_PARENT. 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.