12
votes

In Neo4j 2.1.6, I have nodes that are non-unique in respect of a certain property, inputID.

Using Cypher, how do I remove all nodes that are duplicates in terms of a given property, leaving only uniques?

I have tried the following...

MATCH (n:Input)
WITH n.inputID, collect(n) AS nodes
WHERE size(nodes) > 1
FOREACH (n in tail(nodes) | DELETE n)

...but it results in...

Expression in WITH must be aliased (use AS) (line 2, column 6)
"WITH n.inputID, collect(n) AS nodes"
      ^

Thanks,

G

2

2 Answers

17
votes

You're not aliasing that WITH variable. Change this:

WITH n.inputID, collect(n) AS nodes

To this:

WITH n.inputID AS inputID, collect(n) AS nodes
4
votes

As you correctly found out, using tail on a collection will let you remove the duplicates, don't forget to remove relationships before the node (DETACH) and alias the field as FrobberOfBits mentioned:

MATCH (n:Input)
WITH n.inputID AS inputID, collect(n) AS nodes
WHERE size(nodes) > 1
FOREACH (n in tail(nodes) | DETACH DELETE n)