Neo4j Delete Node

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:GeksforGeeks { 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.



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.