DELETE

The DELETE clause removes graph elements — vertices, edges, or paths.

Note!
Restrictions on DELETE in YMatrix AGE:

  1. You cannot SET properties on a variable after DELETEing it.
    MATCH (n) DELETE n SET n.lol = 'ftw' RETURN n  
    -- ERROR: cannot SET property on deleted variable n  
  2. You cannot CREATE an edge referencing a variable that was DELETEd in the same query.
    MATCH (a)-[e:KNOWS]->(b) DELETE e CREATE (a)-[:KNOWS {reason: 'new'}]->(b)  
    -- ERROR: AssignTransactionId() called by Segment Reader process  

    Recommendation: Split DELETE and subsequent write operations into separate Cypher statements.

Terminal DELETE Clauses

A DELETE clause not followed by any other clause is called a terminal clause. When a Cypher query ends with a terminal clause, the cypher() function returns no rows. However, the function call still requires a column definition list — for example, AS (v agtype). When the query ends with a terminal clause, you may define any dummy column name; it will not contain data.

Introduction

To remove properties, see REMOVE.

You cannot delete a vertex without also deleting all edges for which it serves as either start or end vertex. Either explicitly delete those edges first, or use DETACH DELETE.

Deleting Isolated Vertices

To delete a vertex, use the DELETE clause.

Query

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

This deletes vertices labeled Useless that have no incident edges. The query returns no data.

v
(0 rows)

Deleting All Vertices and Their Incident Edges

Use MATCH to bind vertices, then apply DETACH DELETE to first remove all incident edges and then delete the vertices themselves.

Query

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

This query returns no data.

v
(0 rows)

Deleting Edges Only

To delete an edge, first MATCH it, then include its bound variable in the DELETE clause.

Query

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

This query returns no data.

v
(0 rows)

Returning Deleted Vertices

You may return a deleted vertex using RETURN.

Query

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

Result

v
{"id": 281474976710659, "label": "", "properties": {"name": "A"}}::vertex
(1 row)