Delete a Relationship using Cypher in Neo4j - neo4j tutorial - graph database



How to Delete Relationship using Cypher in Neo4j?

  • You can delete relationships in the same way as deleting nodes - by matching the relationship/s you want to delete.
  • You can delete one or many relationships in one go. You can even delete all relationships in the database.
  • First, as a memory refresher, here are the relationships that we created earlier:
 Neo4j delete a relationship using cypher
  • Let's delete the relationship of type RELEASED.
  • There are several ways we could go about this. Let's look at three.
  • The following statement is quite broad - it will delete all relationships of type RELEASED:
MATCH ()-[r:RELEASED]-() 

DELETE r
  • You could also be more specific and write something like this:
MATCH (:Artist)-[r:RELEASED]-(:Album) 

DELETE r
  • The above statement will match all Artist nodes that have a relationship type of RELEASED with an Album node.
  • You could be even more specific and do something like this:
MATCH (:Artist {Name: "Strapping Young Lad"})-[r:RELEASED]-(:Album 

{Name: "Heavy as a Really Heavy Thing"}) 

DELETE r
  • Any of those statements will result in the RELEASED relationship being deleted. The graph will look like this:
 Neo4j delete a relationship using cypher
Relative Tags : neo , neo4j , graph database , neo4j cypher , neo4j python , neo4j tutorial , neo4j download , neograft

Deleting Nodes with Relationships Attached

  • Nodes can't be deleted if they still have relationships attached to them.
  • If we try to run the following statement:
MATCH (a:Artist {Name: "Strapping Young Lad"}) DELETE a
  • We will get the following error:
 Neo4j delete a node using cypher
  • This is because that node has a relationship connected.
  • One option is to delete all relationships, then delete the node.
  • Another option is to use the DETACH DELETE clause. The DETACH DELETE clause lets you delete a node and all relationships connected to it.
  • So we can change the above statement to this:
MATCH (a:Artist {Name: "Strapping Young Lad"}) DETACH DELETE a
  • Running that statement will result in the following success message:
 Neo4j deleterelationship using cypher

Delete the Whole Database

  • You can take the DETACH DELETE a step further and delete the whole database.
  • Simply remove any filtering criteria and it will delete all nodes and all relationships.
  • Go ahead and execute the following statement:
MATCH (n) DETACH DELETE n

This neo4j tutorial site provides you all the following key points and informations on neograft , neo technology , graphdb , neo4j tutorial , neo4j download , neo4j graph database , open source graph database , neo4j database , neo 4 j , nosql graph database , graph database comparison , best graph database , graphical database , graph database examples , neo database , graph database open source , in memory graph database , database graph , graph based database , graph database neo4j , neo4j pricing , neo4j graph , neo4j example , neo4j performance , neo4j license , graph data model , graph oriented database , neo4j enterprise pricing , neo4j create database , neo4j create new database , neo4j enterprise , neo4j ruby , neo4j node , neo4j with , neo4j start , mysql graph database , neo4j online , graph store , neo4j plugins , neo4j create , neo4j where , neo4j version , neo4j architecture , start neo4j , allegrograph , open source graph , graph database tutorial , neo4j query , neo4j book , what is graph database , neo4j training , apache graph database , neo4j rest api , google graph database , neo4j vs mongodb , download neo4j , python graph database , cypher neo4j , what is neo4j , neo for j , neo4j manual , neo4j spatial , graph database python , neo4j cluster , neo4j demo , neo4j wiki , neo4j docs , neo4j documentation , install neo4j , neo4j github , neo4j data modeling , mongodb vs neo4j , neo4j install , neo4j community , neo4j scalability , infinite graph , neo4j cypher tutorial , neo4j getting started , neo4j console , neo4j open source , neo4j community edition , neo4j logo , world of graphs , neo4j hosting , neo4j vs , neo4j query language , neo j , neo4j driver , neo4j client

Related Searches to Delete a Relationship using Cypher in Neo4j