Cypher provides two clauses to delete data. They are as follows:
To remove a label from a node, you must use the REMOVE
clause. The syntax is similar to the one for the SET
clause, as shown in the following query:
MERGE (b:User {name: "Jack", surname: "Smith"}) REMOVE b:Inactive:MustConfirmEmail
This query removes the labels Inactive
and MustConfirmEmail
from the node that was matched. Note that we have chained the labels using the colon separator. If the node already doesn't have one or all of the labels specified, this query will not fail; it will only remove the labels it can remove.
The REMOVE
clause is the opposite of the SET
clause. It can be used to remove a property from a node, as shown in the following query:
MERGE (b:User {name: "Jack", surname: "Smith"}) REMOVE b.age
Anyway, as Neo4j does not store NULL
properties, the same result can be achieved by setting the property to NULL
, as shown in the following query:
MERGE (b:User {name: "Jack", surname: "Smith"}) SET b.age = NULL
The preceding query can be used effectively when working with parameters. In fact, you can write the query as following:
MERGE (b:User {name: {name}, surname: {surname}}) SET b.age = {age}
This query can be used to both set the age
property of a user and remove the age
parameter from the node. Again, all operations with the REMOVE
and SET
clauses are idempotent, so you don't need to worry if the properties exist before you remove them.
If you want to delete a node, use the DELETE
clause, as shown in the following query:
MATCH (c:User {name: "Mei", surname: "Weng"}) DELETE c
This query looks for a node with the given name and surname using the MATCH
clause and then tries to delete it.
Three important points about the preceding query are as follows:
Therefore, before deleting a node, you must be sure that it is not involved in any relationship. If it is, then how to delete a relationship? The syntax is the same; you just have to use the right variable, as shown in the following query:
MATCH (:User {name: "Mei", surname: "Weng"}) -[r]- ()
DELETE r
This query deletes any relationship that involves the user that was matched. So, to delete a node along with all its relationships, should we perform two queries? Not at all, we just need the following query:
MATCH (c:User {name: "Jack", surname: "Smith"}) OPTIONAL MATCH (c)-[r]- () DELETE r, c
This query deletes the node and any of its relationships. It, first of all, finds the node, then it matches it with any existing relationship. The OPTIONAL MATCH
clause is needed because if we use a simple MATCH
clause, the query won't delete a node with zero relations. Finally, the DELETE
clause causes the relations and the node to be deleted.
Generalizing the preceding query, we can clear the whole database, deleting all the nodes and relationships, just by changing the first MATCH
clause to take all user nodes as argument, as shown in the following query:
MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r
We can also do this by using the START
clause on all the nodes. We get the same result. The query is as follows:
START a = node(*) OPTIONAL MATCH (a)-[r]-() DELETE a, r
Use it carefully because it will delete all of the data from the graph.
13.59.200.206