Deleting data from Neo4j using the Cypher query

In this recipe, we will learn how to delete nodes and relationships from Neo4j. This recipe can be used to delete all nodes/relations or subsets of them.

Getting ready

To step through this recipe, you will need to create some nodes and relationships among them to get the most from this recipe.

How to do it...

We have divided this recipe into the following problem sets:

  • Deleting all relationships from the Neo4j graph:
    MATCH (n)-[r]-() DELETE r
  • Deleting all nodes from the Neo4j graph:
    MATCH (n) DELETE n

    The preceding query will only work if there are no relationships in the graph.

  • Deleting all nodes from the Neo4j graph matching a condition:
    MATCH (n) WHERE n.city = "Atlanta" DELETE n # You have to delete all ...

Get Neo4j Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.