Open In App

Neo4j Delete Node

Last Updated : 06 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Neo4j to delete a node or relations between nodes you have to use DELETE clause. To delete any node you need DELETE clause with the MATCH statement, the MATCH statement data will find the specific node and whichever node is matched with the statement that node will be vanished. Similar to the CREATE and RETURN statement just it will replace the CREATE with MATCH and RETURN with the DELETE. Below examples will illustrate the approach. Example: The basic way to delete a node is to find the node and delete the match node, but before you can check the node is that the node you want to delete by using RETURN statement after that you can fire below query. This query will delete the node where label is “GeeksforGeeks” Tag is “A Computer Science Portal” and the type is “Edutech”.

  • To delete node:
$ MATCH (a:GeeksforGeeks { Tag : "A Computer Science Portal", Type : "Edutech" }) 
DELETE a
  • Output:

Deleting multiple nodes: To delete multiple nodes use the DELETE statement and separate the nodes by “, ” coma or you can use multiple time MATCH statement Like the below query.

  • Multiple nodes deleted: 
$ DELETE (a:GeeksforGeeks { Tag: "A Computer Science Portal"}), 
(b:W3School { Tag: "We are the Learner"}) 
MATCH a, b
  • or
$ MATCH (a:GeeksforGeeks { Tag: "A Computer Science Portal"}) 
MATCH (b:W3School { Tag: "We are the Learner"}) 
DELETE a, b
  • Output:

Delete all nodes: To delete all the nodes at once is to much short query to fire the below query will delete all the nodes at once.

  • Delete all nodes:
$ MATCH (n) DELETE n
  • Output:

Note: To delete any node or nodes that containing relationship with other nodes will display an error message.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads