O: Deleting Nodes and Relationships
Deleting nodes and relationships, using the DELETE clause in Cypher, is an important operation in managing the graph database.
Deleting Nodes
The general syntax for deleting a node is:
MATCH (n:NodeLabel {propertyName: propertyValue})
DELETE n
Where:
nis the node variableNodeLabelis the label assigned to the nodepropertyNameis the property namepropertyValueis the value assigned to the propertyDELETEis used to delete the nodeMATCHis used to find the node to delete...represents additional properties
Example: Deleting a Student Node
MATCH (s:Student {studentID: '123456'})
DELETE s
Deleting Relationships
The general syntax for deleting a relationship is:
MATCH (n1:NodeLabel1)-[r:RELATIONSHIP_TYPE]->(n2:NodeLabel2)
DELETE r
Where:
n1andn2are the node variablesNodeLabel1andNodeLabel2are the labels assigned to the nodesris the relationship variableRELATIONSHIP_TYPEis the type of relationshipDELETEis used to delete the relationshipMATCHis used to find the relationship to delete...represents additional properties
Example: Deleting a Relationship Between a Student and an Activity
MATCH (s:Student {studentID: '123456'})-[r:ATTENDS]->(a:Activity {activityID: '789'})
DELETE r