To delete an object, we will use the DELETE statement:
- For a relationship:
MATCH ()-[r:REL_TYPE {id: 1}]-()
DELETE r
- For a node:
MATCH (n {id: 1})
DELETE n
Deleting a node requires it to be detached from any relationship (Neo4j can't contain a relationship with a NULL extremum).
If you try to delete a node that is still involved in a relationship, you will receive a Neo.ClientError.Schema.ConstraintValidationFailed error, with the following message:
Cannot delete node<41>, because it still has relationships. To delete this node, you must first delete its relationships.
We need to first delete the relationship, and then the node in this way:
MATCH (n {id:1})-[r:REL_TYPE]-()
DELETE r, n
But here again, Cypher provides a practical shortcut for this – DETACH DELETE – which will perform the preceding operation:
MATCH (n {id: 1})
DETACH DELETE n
You now have all the tools to in hand to create, update, delete, and read simple patterns from...