Neo4j Delete a Node

In Neo4j, DELETE statement is always used with MATCH statement to delete whatever data is matched. The DELETE command is used in the same place we used the RETURN clause in our previous examples.

Example

Neo4j Delete a node 1

Delete Multiple Nodes

You can delete multiple nodes by using MATCH and DELETE commands in a single statement. You just have to put the different nodes separated by a column.

Suppose you have these two nodes:

Neo4j Delete a node 2

Use the following command to delete the both nodes.

Neo4j Delete a node 3

You can see the above displayed message that both nodes have been deleted.


Delete All Nodes

To delete all nodes from the database, don?t use any filter criteria.

Note: The above statement cannot delete nodes if they have any relationships. In other words, you must delete any relationships before you delete the node itself. Otherwise you will get the following error message.

Neo4j Delete a node 4

There is a method to delete a node and all relationships related to that node.

Use the DETACH DELETE statement:

Example:

Output:

Neo4j Delete a node 5

This will delete the node "Kohli" where name is "Virat Kohli" with all its relationship.






Latest Courses