Understanding Deletion in Apache AGE

safi50

Safi

Posted on May 12, 2023

Understanding Deletion in Apache AGE

Deletion in AGE is handled via the DELETE clause, which is used to delete graph elements - nodes, relationships or paths. This operation is quite critical and understanding how it works will help you maintain the integrity of your graph.

Deleting a Single Vertex

To delete a vertex, you can use the DELETE clause in conjunction with the MATCH clause to specify which vertex you want to delete. Here's an example:

SELECT * 
FROM cypher('graph_name', $$
    MATCH (v:Useless)
    DELETE v
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

This query will delete the vertex with the label "Useless". But what happens when the MATCH clause matches multiple vertices?

In AGE, the DELETE clause will delete all vertices that match the condition in the MATCH clause. So, if multiple vertices have the label "Useless", all of them will be deleted.

Deleting All Vertices and Edges

In some cases, you may want to delete a vertex and all its connected edges. This can be done using the DETACH DELETE clause.

Here's an example:

SELECT * 
FROM cypher('graph_name', $$
    MATCH (v:Useless)
    DETACH DELETE v
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

This will delete the vertex and all edges connected to it. If multiple vertices match the MATCH clause, all of them and their associated edges will be deleted.

Deleting Edges Only

To delete an edge, use the MATCH clause to find your edges, then add the variable to the DELETE clause. Here's an example:

SELECT * 
FROM cypher('graph_name', $$
    MATCH (n {name: 'Andres'})-[r:KNOWS]->()
    DELETE r
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

This query will delete the edge labeled "KNOWS" that starts from the node named "Andres".

Deleting Vertices when an Edge is Deleted

One common question is whether it's possible to add constraints to a graph to automatically delete vertices when an edge is deleted.

AGE does not support cascading deletes. Deleting an edge will not automatically delete the vertices that it connects. You need to do this manually. Here's how:

SELECT * FROM cypher('graph_name', $$ 
MATCH (a)-[r]->(b) 
WHERE some_condition_on_r 
DELETE r, a, b
$$) as ( a agtype);
Enter fullscreen mode Exit fullscreen mode

This query will find all pairs of nodes (a, b) that are connected by an edge r that meets some condition, and it will delete r, a, and b. Be aware that this will only delete nodes that are not connected by any other edges. If you want to delete a node regardless of whether it has other edges, you would need to delete those edges first.

On the other hand, preserving/retaining the vertices associated with a deleted edge is the default behavior in AGE. When you delete an edge, the vertices it was connecting are not affected.

What Happens to an Edge When All the Vertices Involved Have Been Deleted?

If you delete a vertex, all edges connected to it are also deleted. Therefore, it's not possible for an edge to exist without its associated vertices. If you delete all vertices involved in an edge, the edge is automatically deleted as well. This is in line with the definition of a graph, where an edge is defined by the nodes it connects.

Returning a Deleted Vertex

Interestingly, AGE allows you to return vertices that have been deleted. Here's an example:

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'A'})
    DELETE n
    RETURN n
$$) as (a agtype);
Enter fullscreen mode Exit fullscreen mode

This query will delete the vertex with the name 'A' and return it. The returned value will be the vertex as it existed just before deletion.

Conclusion

  • In summary, Apache AGE provides comprehensive functionality for deleting vertices and edges in a graph. It's important to remember that deleting a vertex will also delete all edges connected to it, and deleting an edge will not affect the vertices it connects. As always, handle deletion operations with care to maintain the integrity of your graph data.

    • Remember that all changes can be committed or rolled back, providing flexibility and safety. AGE's powerful and flexible deletion operations make it a robust tool for managing graph data in a PostgreSQL database.

If you're interested in learning more about how to use Apache AGE, check out the Official Documentation or their GitHub for more detailed information and examples.

Happy graphing!

💖 💪 🙅 🚩
safi50
Safi

Posted on May 12, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related