Search code examples
gremlintinkerpop

Gremlin drop deletes only the first vertex of the sub-graph


I have a graph where A is connected to B,C,D with edges.
I'm using the following to delete A and its connected vertexes.

g.V('A').
  union(__(),
    repeat(out()).emit()).drop()

But it deletes only A.
When I run it without the drop() it returns a list of all the sub-graph vertexes.
I'm using the tinkerpop console/server version 3.4.9.


Solution

  • You need to collect the results of the union step before calling drop. Here is the query slightly modified.

    gremlin> g.addV('A').as('a').
    ......1>   addV('B').as('b').
    ......2>   addV('C').as('c').
    ......3>   addV('D').as('d').
    ......4>   addE('knows').from('a').to('b').
    ......5>   addE('knows').from('b').to('c').
    ......6>   addE('knows').from('c').to('d') 
    ==>e[41][37-knows->38]
    
    
    gremlin>   g.V().hasLabel('A').
    ......1>   union(identity(),
    ......2>     repeat(out()).emit()).fold() 
    ==>[v[35],v[36],v[37],v[38]]
    
    
    gremlin>   g.V().hasLabel('A').
    ......1>   union(identity(),
    ......2>     repeat(out()).emit()).fold().unfold().drop()   
    
    gremlin> g
    
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    

    Note also that this can be done without the need for a union step by moving the emit before the repeat and using store.

    g.V().hasLabel('A').
      emit().
      repeat(store('v').out()).
      cap('v').
      unfold().
      drop()