Deleting Data

Deleting data

In a Neo4j database you can delete:

  • nodes

  • relationships

  • properties

  • labels

To delete any data in the database, you must first retrieve it, then you can delete it. You have already learned how to remove or delete properties from nodes or relationships.

Deleting a node

Suppose you have created a Person node for Jane Doe.

Run this Cypher code to create the node:

cypher
MERGE (p:Person {name: 'Jane Doe'})

You delete this node as follows where you first retrieve the node. Then with a reference to the node you can delete it.

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

Deleting a relationship

Suppose we had our Jane Doe node again where she was added as an actor in the movie, The Matrix. Run this code to create the node and the relationship.

cypher
MATCH (m:Movie {title: 'The Matrix'})
MERGE (p:Person {name: 'Jane Doe'})
MERGE (p)-[:ACTED_IN]->(m)
RETURN p, m

This code creates one node and the relationship from Jane Doe to The Matrix.

To leave the Jane Doe node in the graph, but remove the relationship we retrieve the relationship and delete it.

cypher
MATCH (p:Person {name: 'Jane Doe'})-[r:ACTED_IN]->(m:Movie {title: 'The Matrix'})
DELETE r
RETURN p, m

Run this Cypher code that recreates the relationship:

cypher
MATCH (p:Person {name: 'Jane Doe'})
MATCH (m:Movie {title: 'The Matrix'})
MERGE (p)-[:ACTED_IN]->(m)
RETURN p, m

If we attempt to delete the Jane Doe node, we will receive an error because it has relationships in the graph.

Try running this Cypher code:

cypher
MATCH (p:Person {name: 'Jane Doe'})
DELETE p

You should receive an error. Neo4j prevents orphaned relationships in the graph.

Deleting a node and its relationships

Neo4j provides a feature where you cannot delete a node if it has incoming or outgoing relationships. This prevents the graph from having orphaned relationships.

Run this Cypher code:

cypher
MATCH (p:Person {name: 'Jane Doe'})
DETACH DELETE p

This code deletes the relationship and the Person node.

You can also delete all nodes and relationships in a database with this code.

Do NOT delete all nodes and relationships in the graph as you need them for the next challenge!
cypher
MATCH (n)
DETACH DELETE n

You should only do this on relatively small databases as trying to do this on a large database will exhaust memory.

Deleting labels

A best practice is to have at least one label for a node, but not more than four.

Run this Cypher code to create this Person node in the graph:

cypher
MERGE (p:Person {name: 'Jane Doe'})
RETURN p

Next, run this code to add a new label to this node:

cypher
MATCH (p:Person {name: 'Jane Doe'})
SET p:Developer
RETURN p

To remove the newly-added label, Developer, you use the REMOVE clause. Run this code:

cypher
MATCH (p:Person {name: 'Jane Doe'})
REMOVE p:Developer
RETURN p

The Jane Doe node has two labels, Person and Developer. You can use a MATCH to find that node. Note that you could have specified MATCH (p:Developer {name: 'Jane Doe'}) or MATCH (p:Person:Developer {name: 'Jane Doe'}) to find the same node. Once we have a reference to that node, we can remove the label with the REMOVE clause.

And finally, delete the Jane Doe node by running this code:

cypher
MATCH (p:Person {name: 'Jane Doe'})
DETACH DELETE p

Note that DELETE p would also work in this case since we have not created any relationships.

What labels exist in the graph?

This code returns all node labels defined in the graph.

cypher
CALL db.labels()

Check your understanding

1. Delete an existing node in the graph

Suppose we’ve decided that we do not want the actor River Phoenix in our database who may have relationships to movies in our database.

Complete the code below for removing this actor from the database.

Use the dropdown below complete the code.

cypher
MATCH (p:Person {name: 'River Phoenix'})
/*select:DELETE p*/
  • DELETE p

  • REMOVE p

  • DETACH DELETE p

Once you have selected your option, click the Check Results query button to continue.

Hint

You must delete relationships then the node.

Solution

DETACH DELETE p is the correct answer.

If you were to attempt DELETE p, it would fail because it has relationships and Cypher prevents you from deleting nodes without first deleting its relationships.

There is no REMOVE p clause in Cypher.

2. Cleaning up graph

Which Cypher code deletes all nodes and relationships from the graph?

  • MATCH (n) DETACH DELETE n

  • DELETE NODES, RELATIONSHIPS

  • MATCH (n) DELETE n

  • DELETE ALL

Hint

Which code retrieves all nodes in the graph? You must delete the nodes safely by deleting the relationships first.

Solution

MATCH (n) DETACH DELETE n is the correct answer as it first deletes relationships to/from a node, then the node.

MATCH (n) DELETE n will fail because you cannot delete nodes without first deleting the relationships.

There are no such clauses DELETE NODES, RELATIONSHIPS and DELETE ALL in Cypher.

Summary

In this lesson, you learned how to delete nodes and relationships from the graph. In the next challenge, you will demonstrate that you can delete a node from the graph.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?