Taylor Hx
Taylor Hx

Reputation: 2833

Remove all labels for Neo4j Node

The following examples are taken from the Neo4j documentation found here.

Using Cypher, it is possible to remove a single, known label using a Cypher statement like so:

MATCH (n { name: 'Peter' })
REMOVE n:German
RETURN n

You can also remove multiple labels like so:

MATCH (n { name: 'Peter' })
REMOVE n:German:Swedish
RETURN n

So how would one remove all labels from a node using simple Cypher statements?

Upvotes: 3

Views: 2454

Answers (3)

Karol Selak
Karol Selak

Reputation: 4804

You can also try this way using doIt method from apoc library:

match (n {name: 'Peter'})
call apoc.cypher.doIt(
  "match (o)" +
  " where ID(o) = " + ID(n) +
  " remove "+reduce(a="o",b in labels(n) | a+":"+b) + 
  " return (o);",
null)
yield value
return value

Upvotes: 3

Dave Bennett
Dave Bennett

Reputation: 11216

so, how about a two step cypher approach? use cypher to generate some cypher statements and then execute your cypher statements in the shell.

You could try something like this to generate the batch cypher statements

match (n) 
return distinct "match (n" 
+ reduce( lbl_str= "", l in labels(n) | lbl_str + ":" + l) 
+ ") remove n" 
+ reduce( lbl_str= "", l in labels(n) | lbl_str + ":" + l) 
+ ";"

The output should look something like this...

match (n:Label_1:Label_2) remove n:Label_1:Label_2;
match (n:Label_1:Label_3) remove n:Label_1:Label_3;
match (n:Label_2:Label_4) remove n:Label_2:Label_4;

You would probably want to remove any duplicates and depending on your data there could be quite a few.

Not exactly what you are looking for but I think it would get you to the same end state using just cypher and the neo4j shell.


Shiny NEW and improved cypher below...

I edited this down to something that would work in the browser alone. It hink this is a much better solution. It is still two steps but it produces a single statement that can be cut and paste into the browser.

match (n) 
with distinct labels(n) as Labels
with reduce(lbl_str="", l in Labels | lbl_str + ":" + l) as Labels
order by Labels
with collect(Labels) as Labels
with Labels, range(0,length(Labels) - 1) as idx
unwind idx as i
return "match (n" + toString(i) + Labels[i] + ")" as Statement
union
match (n) 
with distinct labels(n) as Labels
with reduce(lbl_str="", l in Labels | lbl_str + ":" + l) as Labels
order by Labels
with collect(Labels) as Labels
with Labels, range(0,length(Labels) - 1) as idx
unwind idx as i
return "remove n" + toString(i) + Labels[i] as Statement

which produces output like this...

match (n0:Label_A)
match (n1:Label_B)
match (n2:Label_C:Label_D)
match (n3:Label_E)
remove n0:Label_A
remove n1:Label_B
remove n2:Label_C:Label_D
remove n3:Label_E

which can then be cut and paste into the Neo4j browser.

Upvotes: 1

Eve Freeman
Eve Freeman

Reputation: 33175

There's no syntax for that yet! Labels are usually things that are known quantities, so you can list them all out if you want. There's no dynamic way to remove them all, though.

Upvotes: 1

Related Questions