Override and Replace a node with another existing node?

Is there a way to replace a node with another existing one in neo4j, without having to delete all relationships, delete the old node, and reattach relationships to the new one?

E.g. I have an industry node called online retail that 3 company nodes point to. I want to "switch it out" with a sector node that is called retail. I want all 3 initial companies to point to this new sector retail node. Is there a way to do this, without detaching relationships from the industry node, deleting the industry node, and then adding each company/sector relationship back?


You don't delete the node, you just modify it in place. You can use SET/UNSET to change labels, and then just assign properties. You'd do it like this:

MATCH (x:Industry { name: "online retail" })
UNSET x:Industry
SET x:Sector, x.name = "retail"

When you do this, the relationships don't get touched. I would not advise deleting / recreating the node, because then you would have to rewire the relationships.

1 Like