DEV Community

Cover image for Delete and Detach Delete Command in Apache Age
WALEED SHAHID
WALEED SHAHID

Posted on

Delete and Detach Delete Command in Apache Age

Introduction

Data management is a critical aspect of any database system, and Apache Age provides powerful features to handle data manipulation effectively. Two essential operations for data removal in Apache Age are DELETE and DETACH DELETE. In this article, we will explore these operations and demonstrate their usage through queries and their corresponding results.

DELETE Operation:

The DELETE operation in Apache Age is used to remove specific records from a graph. It permanently deletes the specified vertices and edges from the graph. Let's consider a simple example of a social network graph:

CREATE (alice:Person {name: 'Alice'})
CREATE (bob:Person {name: 'Bob'})
CREATE (charlie:Person {name: 'Charlie'})

CREATE (alice)-[:FRIENDS_WITH]->(bob)
CREATE (alice)-[:FRIENDS_WITH]->(charlie)

Enter fullscreen mode Exit fullscreen mode

Now, if we want to delete the relationship between Alice and Bob, we can use the following query:

DELETE
MATCH (alice:Person {name: 'Alice'})-[r:FRIENDS_WITH]->(bob:Person {name: 'Bob'})
DELETE r
Enter fullscreen mode Exit fullscreen mode

The above query will delete the friendship relationship between Alice and Bob. The result would be as follows:

MATCH (alice:Person {name: 'Alice'})-[r:FRIENDS_WITH]->(bob:Person {name: 'Bob'})
RETURN alice, bob, r
Enter fullscreen mode Exit fullscreen mode

Result:

alice = Person {name: 'Alice'}
bob = Person {name: 'Bob'}
r = NULL
Enter fullscreen mode Exit fullscreen mode

As seen in the result, the relationship r no longer exists.

DETACH DELETE Operation:

The DETACH DELETE operation in Apache Age removes specified vertices and edges from the graph, along with any relationships they have. Unlike DELETE, DETACH DELETE also removes the relationships associated with the deleted vertices. Let's consider the following example:

CREATE (alice:Person {name: 'Alice'})
CREATE (bob:Person {name: 'Bob'})
CREATE (charlie:Person {name: 'Charlie'})

CREATE (alice)-[:FRIENDS_WITH]->(bob)
CREATE (alice)-[:FRIENDS_WITH]->(charlie)
Enter fullscreen mode Exit fullscreen mode

To delete the vertex representing Charlie and any relationships associated with it, we can use the following query:

MATCH (charlie:Person {name: 'Charlie'})
DETACH DELETE charlie
Enter fullscreen mode Exit fullscreen mode

The above query will delete Charlie's vertex and all relationships associated with it.

Error:

Apache Age provides robust error handling mechanisms to ensure data integrity and consistency within graph databases. When attempting to delete a vertex that has existing edges connected to it, the DELETE operation in Apache Age will raise an error. In this article, we will explore this scenario and explain how to handle such errors gracefully.

Let's consider a social network graph example.

CREATE (alice:Person {name: 'Alice'})
CREATE (bob:Person {name: 'Bob'})
CREATE (charlie:Person {name: 'Charlie'})

CREATE (alice)-[:FRIENDS_WITH]->(bob)
CREATE (alice)-[:FRIENDS_WITH]->(charlie)
Enter fullscreen mode Exit fullscreen mode

Now, if we try to delete the vertex representing Alice using the DELETE operation, which has existing edges connected to it, Apache Age will raise an error. The following query illustrates this scenario:

DELETE
MATCH (alice:Person {name: 'Alice'})
DELETE alice
Enter fullscreen mode Exit fullscreen mode
Error: Unable to delete node `alice` because it still has relationships.
Enter fullscreen mode Exit fullscreen mode

As seen in the result, Apache Age raises an error stating that the deletion is not possible due to existing relationships connected to the vertex.

Error Handling:

To handle this error gracefully, we can use the DETACH DELETE operation instead of DELETE. The DETACH DELETE operation will remove the specified vertex and its associated relationships. Let's modify the query to use DETACH DELETE:

MATCH (alice:Person {name: 'Alice'})
DETACH DELETE alice
Enter fullscreen mode Exit fullscreen mode

By using DETACH DELETE, Apache Age removes the vertex and its relationships without raising an error, ensuring data consistency within the graph.

Conclusion:

In Apache Age, attempting to delete a vertex with existing edges using the DELETE operation will raise an error. To handle this situation gracefully, it is recommended to use the DETACH DELETE operation, which removes the vertex along with its relationships. By understanding how to handle errors effectively, developers and database administrators can maintain the integrity of their graph databases and handle data removal operations seamlessly.

Top comments (0)