Understanding the REMOVE Clause in Apache AGE
Tags: apache, apacheage, postgres, database
Published: 2023-06-02 16:29:12

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)

```

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)
```
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)
```
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](https://dev.to/dukeofhazardz/how-to-perform-delete-and-detach-delete-in-apache-age-4m1b)_

---

### 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
- [The REMOVE clause](https://age.apache.org/age-manual/master/clauses/remove.html)
- [How to Perform DELETE and DETACH DELETE in Apache AGE](https://dev.to/dukeofhazardz/how-to-perform-delete-and-detach-delete-in-apache-age-4m1b)
- Visit Apache AGE Website: https://age.apache.org/
- Visit Apache AGE GitHub: https://github.com/apache/age
- Visit Apache AGE Viewer GitHub: https://github.com/apache/age-viewer