DEV Community

Cover image for Understanding the Apache AGE DELETE Clause.
Chidera Stella Onumajuru
Chidera Stella Onumajuru

Posted on

Understanding the Apache AGE DELETE Clause.

Introduction

Apache AGE(AGE) is a PostgreSQL extension that enables hybrid queries using PostgreSQL. The AGE DELETE clause is used to delete Vertices and edges.

Prerequisites

Before following this tutorial, make sure you have:

  1. Installed PostgreSQL from source code. Here's a guide on installing PostgreSQL.
  2. Installed and set up AGE from source code. Here's a guide on installing AGE.
  3. Basic knowledge of Vertices and Edges in graph databases. Here's a beginner's guide to get you started.

The graph that is being queried in this tutorial has been created in a previous tutorial here and its contents it as follows.

An image showing the vertices we are working with

An image showing the edges we are working with

  • To delete a vertex, it has to be selected using the MATCH clause, with its contents stored in a variable and then the obtained variable will be deleted using the DELETEclause, here is a sample query below.
SELECT * FROM cypher('food', $$ MATCH (v:Recipes) DELETE v $$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

An image showing the query result

  • Deleting this vertex made us encounter an error, this error occurred because the Vertex specified above have an edge, using DETACH DELETE will get the two deleted as can be seen below.
SELECT * FROM cypher('food', $$ MATCH (v:Recipes) DETACH DELETE v RETURN v $$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

An image showing the query result

  • The DELETE clause can be used to delete an edge, as can be seen in the query below.
SELECT * FROM cypher('food', $$ MATCH (n:food_blog {name: 'Tasty Delight'})-[r:HAS_CONTENT]->(m:Meal_planning) DELETE r RETURN r $$) as (v agtype);

Enter fullscreen mode Exit fullscreen mode

An image showing the query result

  • Now we can DELETE the remaining vertex with the code below.
SELECT * FROM cypher('food', $$ MATCH (x:Meal_planning) DELETE v,x RETURN x $$) as (v x agtype);
Enter fullscreen mode Exit fullscreen mode

An image showing the query result
Conclusion

Apache AGE DELETE clause helps in removing a vertex or an edge from a graph. Understanding its operation will help one to manipulate a graph better.

Top comments (0)