Search code examples
gremlintinkerpoptinkerpop3amazon-neptune

Proper handling of vertex deletion in AWS Neptune Gremlin Python


Deleting a vertex in AWS Neptune using gremlin_python throws an error, even though the vertex is actually deleted (cannot find it when I query for it later).

Is my vertex deletion query wrong?

delete_vertex_q = "g.V(%s).drop().next()"%(v_id)
delete_vertex_r = eval(delete_vertex_q)

after that when i try to find the vertex again:
print(g.V('<vertex_id>').next())
I get a StopIteration error.

How should I properly handle deletion and checking of vertices in AWS Neptune Gremlin?


Solution

  • You need to think of traversals as a stream and when that stream is empty and you try to get the next one an error will occur. If you are sending scripts to Neptune then g.V(id).hasNext() is the syntax that will get you a boolean result. If you are using gremlin-python and writing Gremlin in native Python then you will need to do something like:

    len(g.V(id).toList()) == 0
    

    or perhaps:

    g.V(id).count().next() == 0
    

    Note that with the (soon to be released) gremlin-python 3.2.10/3.3.4 you can do hasNext() in Python directly and the above to options can be ignored.