Deleting vertices with associated edges is currently not handled via AQL while the graph management interface and the REST API for the graph module offer a vertex deletion functionality. However, as shown in this example based on the Knows Graph, a query for this use case can be created.

The knows_graph example graphFive persons as nodes, connected by knows edges: eve knows alice and bob, alice knows bob, and bob knows charlie and dave.evealicebobcharliedave

When deleting vertex eve from the graph, we also want the edges eve -> alice and eve -> bob to be removed. The involved graph and its only edge collection has to be known. In this case it is the graph knows_graph and the edge collection knows.

This query will delete eve with its adjacent edges:

LET edgeKeys = (FOR v, e IN 1..1 ANY 'persons/eve' GRAPH 'knows_graph' RETURN e._key)
LET r = (FOR key IN edgeKeys REMOVE key IN knows) 
REMOVE 'eve' IN persons
Show output
[ ]

This query executed several actions:

  • use a graph traversal of depth 1 to get the _key of eve’s adjacent edges
  • remove all of these edges from the knows collection
  • remove vertex eve from the persons collection

The following query shows a different design to achieve the same result:

LET edgeKeys = (FOR v, e IN 1..1 ANY 'persons/eve' GRAPH 'knows_graph'
            REMOVE e._key IN knows)
REMOVE 'eve' IN persons
Show output
[ ]

Note: The query has to be adjusted to match a graph with multiple vertex/edge collections.

For example, the City Graph contains several vertex collections - germanCity and frenchCity and several edge collections - french / german / international Highway.

The routeplanner example graphGerman cities (Berlin, Cologne, Hamburg) in the germanCity collection and French cities (Lyon, Paris) in the frenchCity collection, connected by highways with a distance: germanHighway edges from Berlin to Cologne (850) and Hamburg (400) and from Hamburg to Cologne (500), a frenchHighway edge from Paris to Lyon (550), and internationalHighway edges from Berlin to Lyon (1100) and Paris (1200), from Hamburg to Paris (900) and Lyon (1300), and from Cologne to Lyon (700) and Paris (550).850400500550110012009001300700550HamburggermanCityBerlingermanCityColognegermanCityParisfrenchCityLyonfrenchCitygermanHighway, frenchHighwayinternationalHighway

To delete city Berlin all edge collections french / german / international Highway have to be considered. The REMOVE operation has to be applied on all edge collections with OPTIONS { ignoreErrors: true }. Not using this option will stop the query whenever a non existing key should be removed in a collection.

LET edgeKeys = (FOR v, e IN 1..1 ANY 'germanCity/Berlin' GRAPH 'routeplanner' RETURN e._key)
LET r = (FOR key IN edgeKeys REMOVE key IN internationalHighway
        OPTIONS { ignoreErrors: true } REMOVE key IN germanHighway
        OPTIONS { ignoreErrors: true } REMOVE key IN frenchHighway
        OPTIONS { ignoreErrors: true }) 
REMOVE 'Berlin' IN germanCity
Show output
[ ]