DEV Community

Cover image for Understanding the REMOVE Clause in Apache AGE
Nnaemeka Daniel John
Nnaemeka Daniel John

Posted on

Understanding the REMOVE Clause in Apache AGE

Apache AGE is a graph database built on PostgreSQL and it provides a powerful toolset to manage and analyze data while ensuring privacy. One of its key features is the REMOVE clause, which allows users to selectively remove sensitive information from their datasets.
In this post, we will delve into the functionalities of the REMOVE clause in Apache AGE.


The REMOVE Clause

The REMOVE clause in Apache AGE enables users to delete specific properties or entire vertices from a graph while preserving the rest of the data. It provides a fine-grained control mechanism, allowing for the removal of sensitive information without affecting the overall structure and integrity of the graph.


Performing queries using the REMOVE clause

Given a graph with the following properties;

demo=# SELECT * FROM create_graph('persons');
NOTICE:  graph "persons" has been created
 create_graph
--------------

(1 row)

demo=# SELECT * FROM cypher('persons', $$ CREATE (:Person {name: 'Peter', age: 30}),
demo$# (:Person {name: 'Jamie', age: 23}), (:Person {name: 'Grace', age: 21}) $$)
demo-# as (a agtype);
 a
---
(0 rows)

demo=# SELECT * FROM cypher('persons', $$ MATCH (u)
demo$# RETURN u $$) as (a agtype);
                                               a
------------------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Person", "properties": {"age": 30, "name": "Peter"}}::vertex
 {"id": 844424930131970, "label": "Person", "properties": {"age": 23, "name": "Jamie"}}::vertex
 {"id": 844424930131971, "label": "Person", "properties": {"age": 21, "name": "Grace"}}::vertex
(3 rows)

Enter fullscreen mode Exit fullscreen mode

We can decide to remove the age property from 'Peter'

demo=# SELECT * FROM cypher('persons', $$ MATCH (peter {name: 'Peter'})
demo$# REMOVE peter.age RETURN peter $$) as (peter agtype);
                                        peter
-------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Person", "properties": {"name": "Peter"}}::vertex
(1 row)
Enter fullscreen mode Exit fullscreen mode

As you can see the age attribute of 'Peter' is now missing from the graph.


Another way this can be done is to filter the search using the WHERE clause.

demo=# SELECT * FROM cypher('persons', $$ MATCH (peter)
WHERE peter.name = 'Peter' REMOVE peter.age
RETURN peter $$) as (peter agtype);
                                        peter
-------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Person", "properties": {"name": "Peter"}}::vertex
(1 row)
Enter fullscreen mode Exit fullscreen mode

The node is returned, and no property age exists on it.

If you'd like to know how to delete vertices and edges from a graph database Look up this post How to Perform DELETE and DETACH DELETE in Apache AGE


Conclusion

Apache AGE's REMOVE clause is a powerful tool for managing and safeguarding sensitive data within a graph database and by utilizing the REMOVE clause effectively, you can selectively delete specific properties or entire vertices, thus preserving privacy and ensuring compliance with data protection regulations.


References

Top comments (0)