neo4j
  1. neo4j-delete-a-relationship

Delete a Relationship

In Neo4j, a relationship can be deleted using the DELETE clause in Cypher Query Language (CQL). This can be useful when you want to remove a relationship between two nodes in your graph database.

Syntax

The syntax for deleting a relationship in Neo4j using CQL is as follows:

MATCH (node1)-[relationship]->(node2)
DELETE relationship

Where:

  • node1 and node2 are the nodes that are connected by the relationship you want to delete.
  • relationship is the relationship type that you want to delete.

Example

Consider the following example graph:

Neo4j graph example

Suppose we want to delete the FRIEND relationship between nodes A and B. We can use the following CQL query:

MATCH (A)-[r:FRIEND]->(B)
DELETE r

This will delete the FRIEND relationship between nodes A and B.

Output

The output of the query will not display anything. Instead, it will simply delete the specified relationship.

Explanation

In the example above, we used the MATCH clause to find the relationship we want to delete between nodes A and B. We then used the DELETE clause to remove the relationship from the graph.

Use

Deleting a relationship in Neo4j is useful when you want to remove a relationship between two nodes in your graph database. This can be helpful when you want to update the relationship between two nodes or remove old or irrelevant data from your graph database.

Important Points

  • Before deleting a relationship, make sure you have identified the correct nodes and relationship type.
  • Deleting a relationship will not delete any nodes in your graph database.
  • Deleting a relationship will affect any queries that rely on that relationship.

Summary

In Neo4j, a relationship can be deleted using the DELETE clause in CQL. By specifying the two nodes and relationship type, you can remove a relationship from your graph database. This is useful when you want to update or remove old or irrelevant data from your graph. However, it is important to double-check your query as deleting a relationship can have an impact on any queries that rely on it.

Published on: