DEV Community

Muhammad Muneeb Ur Rehman
Muhammad Muneeb Ur Rehman

Posted on

Delete Vertices (Nodes) from Apache AGE

In Apache AGE, we cannot delete a node without also deleting edges that start or end on said vertex. Either explicitly delete the vertex's edges first and then delete the vertex, or use DETACH DELETE to delete vertex's edges first and then delete the vertex.

Delete single vertex:
Here is the simple query for deleting a vertex if it has no edge.
Query:
SELECT *
FROM cypher('graph_name', $$
MATCH (v:Useless)
DELETE v
$$) as (v agtype);

Explanation:
This query will match vertices with label Useless by MATCH (v:Useless) and then delete them using DELETE v.

Using DETACH DELETE:
If we have some edges associated with the vertex we want to delete. We can use DETACH DELETE to delete all the edges related to the vertex and the vertex itself.

Query:
SELECT *
FROM cypher('graph_name', $$
MATCH (v:Useless)
DETACH DELETE v
$$) as (v agtype);

Explanation:
The above Query first match the vertex to be deleted by using MATCH (v:Useless), then use DETEACH DELETE to first delete a vertex’s edges then delete the vertex itself.

Return a deleted vertex:
SELECT *
FROM cypher('graph_name', $$
MATCH (n {name: 'A'})
DELETE n
RETURN n
$$) as (a agtype);

Explanation:
The above Query first match the vertex to be deleted by using MATCH (n {name: 'A'}), then delete (DELETE n) the vertex and also returns the deleted vertex using RETURN n.

For more details visit: https://age.apache.org/overview/

AGE Github Link: https://github.com/apache/age

AGE Viewer Github Link: https://github.com/apache/age-viewer

Top comments (0)