--liquibase formatted cypher -- similar to https://github.com/theorchard/lambda-kinesis-to-neo4j/blob/master/lambda/kinesis-to-neo4j/src/logic/art_relations/queries/queries.py#L1469-L1484 -- but no need to check type of relationship since this track_artist id should not exists. --changeset npatel:1 runAlways:true MATCH (ta:TrackArtist) where ta.id in [999999900, 999999901, 999999007, 999999000] MATCH (ta)-[:CREATED_FROM]-(lp:LabelParticipant) OPTIONAL MATCH (lp)-[pit:PARTICIPATED_IN]-> (t:Track:Orchard) WITH *, [node IN [ta, lp, t] WHERE node IS NOT NULL] AS nodesToLock CALL apoc.lock.nodes(nodesToLock) OPTIONAL MATCH (lp)-[:CREATED_FROM]-(sameTa:TrackArtist) WHERE sameTa.id <> ta.id FOREACH (noDup IN CASE WHEN sameTa IS NULL THEN [1] ELSE [] END | DETACH DELETE pit, ta ) FOREACH (dup IN CASE WHEN sameTa IS NOT NULL THEN [1] ELSE [] END | DETACH DELETE ta )