Deleting Nodes and Relationships

About this module

You have just learned how to create nodes, relationships, and properties for them. Next, you will learn how to delete nodes and relationships in the graph.

At the end of this module, you will write Cypher statements to:

  • Delete a node.

  • Delete a relationship.

  • Delete a node and its relationships.

Because the code examples in this lesson modify the database, it is recommended that you do not execute them against your database as you will be doing so in the hands-on exercises.

Deleting nodes

Suppose you have created a Person node for Jane Doe:

CREATE (p:Person {name: 'Jane Doe'})

You delete this node as follows:

MATCH (p:Person)
WHERE p.name = 'Jane Doe'
DELETE p

Here is the result:

DeleteJaneDoe

You can delete a node provided:

  1. You can obtain a reference to it (typically using MATCH).

  2. The node has no relationships.

So if you accidentally created multiple nodes in the graph, you can retrieve them with a MATCH clause and delete them.

Deleting relationships

In our graph, the Katie Holmes node already has an :ACTED_IN relationship with the Batman Begins movie. Perhaps we were over zealous with the creation of relationships between nodes. For example, what if we had this code where relationships were created:

MATCH (a:Person), (m:Movie)
WHERE a.name = 'Katie Holmes' AND m.title = 'Batman Begins'
CREATE (a)-[:WROTE]->(m)
CREATE (a)-[:DIRECTED]->(m)
WITH a
MATCH (a)-[rel]-()
RETURN type(rel)

Here is the result of executing this code:

TooManyRels

We see that there are now three relationships from Katie Holmes and Batman Begins.

Example: Finding and deleting relationships

Provided you have a reference to a relationship, you can delete it. So for example, we can find the relationships that we want to delete and then delete them as follows:

MATCH (a:Person)-[rel:WROTE | DIRECTED]->(m:Movie)
WHERE a.name = 'Katie Holmes' AND m.title = 'Batman Begins'
DELETE rel
RETURN a, m

Here is the result of executing this code:

DeleteRels

Example: Confirming deletion

You can confirm that there is only one relationship associated with the Katie Holmes node as follows:

MATCH (a:Person)-[rel]-()
WHERE a.name = 'Katie Holmes'
RETURN count(rel) AS `Number of Katie Holmes relationships:`

Here is the result of executing this code:

KatieRels

Deleting nodes and relationships

The most efficient way to delete a node and its corresponding relationships is to specify DETACH DELETE. When you specify DETACH DELETE for a node, the relationships to and from the node are deleted, then the node is deleted.

If we were to attempt to delete the Liam Neeson node without first deleting its relationships:

MATCH (p:Person)
WHERE p.name = 'Liam Neeson'
DELETE p

We would see this error:

LiamNeesonDeleteError

Using DETACH DELETE

Here we delete the Liam Neeson node and its relationships to any other nodes:

MATCH (p:Person)
WHERE p.name = 'Liam Neeson'
DETACH DELETE  p

Here is the result of running this Cypher statement:

DeleteLiamNeeson

And here is what the Batman Begins node and its relationships now look like. There is only one actor, Michael Caine connected to the movie.

BatmanBeginsRelationships

Exercise 11: Deleting nodes and relationships

In the query edit pane of Neo4j Browser, execute the browser command:

:play 4.0-intro-neo4j-exercises

and follow the instructions for Exercise 11.

This exercise has 6 steps. Estimated time to complete: 10 minutes.

Check your understanding

Question 1

You can delete a node with the DELETE clause. What must you do before executing the DELETE clause?

Select the correct answers.

  • Get a reference to the node.

  • Ensure the node has no relationships.

  • Remove all labels from the node.

  • Remove all properties from the node

Question 2

What happens if you attempt to delete a node using DELETE, and the node has existing relationships?

Select the correct answers.

  • You receive a client error.

  • The node is deleted.

  • The node’s relationships are deleted.

  • Nothing is changed in the graph.

Question 3

Suppose you retrieve a node, n in the graph that is related to other nodes. What Cypher clause do you write to delete this node and its relationships in the graph?

Select the correct answer.

  • DELETE n

  • DELETE n WITH RELATIONSHIPS

  • REMOVE n

  • DETACH DELETE n

Summary

You can now write Cypher statements to:

  • Delete a node.

  • Delete a relationship.

  • Delete a node and its relationships.