The DELETE clause is used to delete graph elements—nodes, relationships orpaths.

Terminal DELETE clauses

A DELETE clause that is not followed by another clause is called a terminal clause. When a cypher query ends with a terminal clause, no results will be returned from the cypher function call. However, the cypher function call still requires a column list definition. When cypher ends with a terminal node, define a single value in the column list definition: no data will be returned in this variable.

Introduction

For removing properties, see REMOVE.

You cannot delete a node without also deleting edges that start or end on said vertex. Either explicitly delete the vertices,or use DETACH DELETE.

Delete isolated vertices

To delete a vertex, use the DELETE clause.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (v:Useless)
  4. DELETE v
  5. $$) as (v agtype);

This will delete the vertices (with label Useless) that have no edges. Nothing is returned from this query.

v
(0 rows)

Delete all vertices and edges associated with them

Running a MATCH clause will collect all nodes— use the DETACH option to first delete a vertice’s edges, then delete the vertex itself.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (v:Useless)
  4. DETACH DELETE v
  5. $$) as (v agtype);

Nothing is returned from this query.

v
(0 rows)

Delete edges only

To delete an edge, use the MATCH clause to find your edges, then add the variable to the DELETE clause.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (n {name: 'Andres'})-[r:KNOWS]->()
  4. DELETE r
  5. $$) as (v agtype);

Nothing is returned from this query.

v
(0 rows)

Return a deleted vertex

You can return vertices that have been deleted with a RETURN clause.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (n {name: 'A'})
  4. DELETE n
  5. RETURN n
  6. $$) as (a agtype);
v
{“id”: 281474976710659, “label”: “”, “properties”: {“name”: “A”}}::vertex
(1 rows)