REMOVE
The REMOVE
clause is used to remove properties from nodes and relationships, and to remove labels from nodes.
For deleting nodes and relationships, see |
Removing labels from a node is an idempotent operation: if you try to remove a label from a node that does not have that label on it, nothing happens. The query statistics will tell you if something needed to be done or not. |
Example graph
The following graph is used for the examples below:
To recreate it, run the following query against an empty Neo4j database:
CREATE
(a:Swedish {name: 'Andy', age: 36, propTestValue1: 42}),
(t:Swedish {name: 'Timothy', age: 25, propTestValue2: 42}),
(p:German:Swedish {name: 'Peter', age: 34}),
(a)-[:KNOWS]->(t),
(a)-[:KNOWS]->(p)
Remove a property
Neo4j doesn’t allow storing null
in properties.
Instead, if no value exists, the property is just not there.
So, REMOVE
is used to remove a property value from a node or a relationship.
MATCH (a {name: 'Andy'})
REMOVE a.age
RETURN a.name, a.age
The node is returned, and no property age
exists on it.
a.name | a.age |
---|---|
|
|
Rows: 1 |
Remove all properties
REMOVE
cannot be used to remove all existing properties from a node or relationship.
Instead, using SET
with =
and an empty map as the right operand will clear all properties from the node or relationship.
Dynamically removing a property
REMOVE
can be used to remove a property on a node or relationship even when the property key name is not statically known.
REMOVE n[key]
The dynamically calculated key must evaluate to a STRING
value.
This query creates a copy of every property on the nodes:
MATCH (n)
WITH n, [k IN keys(n) WHERE k CONTAINS "Test" | k] as propertyKeys (1)
FOREACH (i IN propertyKeys | REMOVE n[i]) (2)
RETURN n.name, keys(n);
1 | The keys() function retrieves all property keys of the matched nodes, and a list comprehension filters these keys to include only those that contain the substring "Test", assigning the resulting list to the variable propertyKeys . |
2 | The FOREACH clause iterates over each key in the propertyKeys list and removes the corresponding property using the REMOVE clause. |
All properties with the word "Test" in them are removed:
n.name | keys(n) |
---|---|
|
|
|
|
|
|
Rows: 3 |
Remove a label from a node
To remove labels, you use REMOVE
.
MATCH (n {name: 'Peter'})
REMOVE n:German
RETURN n.name, labels(n)
n.name | labels(n) |
---|---|
|
|
Rows: 1 |
Dynamically removing a label
REMOVE
can be used to remove a label on a node even when the label is not statically known.
MATCH (n)
REMOVE n:$(expr)
MATCH (n {name: 'Peter'})
UNWIND labels(n) AS label (1)
REMOVE n:$(label)
RETURN n.name, labels(n)
1 | UNWIND is used here to transform the list of labels from the labels() function into separate rows, allowing subsequent operations to be performed on each label individually. |
n.name | labels(n) |
---|---|
|
|
Rows: 1 |
Remove multiple labels from a node
To remove multiple labels, you use REMOVE
.
MATCH (n {name: 'Peter'})
REMOVE n:German:Swedish
RETURN n.name, labels(n)
n.name | labels(n) |
---|---|
|
|
Rows: 1 |
Remove multiple labels dynamically from a node
It is possible to remove multiple labels dynamically using a LIST<STRING>
and/or by chaining them separately with a :
:
MATCH (n {name: 'Peter'})
REMOVE n:$(labels(n))
RETURN n.name, labels(n)
n.name | labels(n) |
---|---|
|
|
Rows: 1 |