1
votes

run this Cypher query twice to create clone of the Actor

CREATE (n:Actor { name:"Tom Hanks" });

Now i have this two nodes, when i ran this query

MATCH (actor:Actor) RETURN actor;

I got the resultset as

actor

(1:Actor {name:"Tom Hanks"})

(2:Actor {name:"Tom Hanks"})

Now how to delete only the 2nd node

3

3 Answers

3
votes

Another possibility:

MATCH (actor:Actor {name: 'Tom Hanks'})
WITH actor
SKIP 1
DELETE actor;
2
votes

You can collect the nodes, loop through the tail of the collection in a for-each loop, and delete each. It works for your case with two "Tom Hanks" nodes, but equally well if you have five or fifty, deleting every except one.

MATCH (a:Actor { name:"Tom Hanks" })
WITH collect (a) as aa
FOREACH (b IN TAIL (aa)) |
    DELETE b)

or with list slicing

MATCH (a:Actor { name:"Tom Hanks" })
WITH collect (a) as aa
FOREACH (b IN aa[1..] |
    DELETE b)
0
votes

if you know the identifier of the node, then you can use that to delete the node.

MATCH (actor:Actor { name: "Tom Hanks" })
WHERE id(actor)=1
DELETE actor;