Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gremlin remove all Vertex

I know how to remove a vertex by id, but I need to delete multiple vertices (clean the db).

Deleting 1 v is like this:

ver = g.v(1)
g.removeVertex(ver)
like image 640
Aleksandrenko Avatar asked Oct 10 '12 07:10

Aleksandrenko


People also ask

How do you get rid of the vertex in Gremlins?

If you have the vertex already then simply v. remove() . Any of the 3 will serve.

What is TinkerPop?

Apache TinkerPop is an open source computing framework for graph databases and graph analytic systems. Designed to appeal to software developers, TinkerPop lets developers add graph computing capabilities to their applications without worrying about developing APIs, graph processing engines, or graph algorithms.


2 Answers

In more recent terms as of Gremlin 2.3.0, removal of all vertices would be best accomplished with:

g.V.remove()

UPDATE: For version Gremlin 3.x you would use drop():

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().drop().iterate()
gremlin> graph
==>tinkergraph[vertices:0 edges:0]

Note that drop() does not automatically iterate the Traversal as remove() did so you have to explicitly call iterate() for the deletion to occur. Iteration in the Gremlin Console is discussed in detail in this tutorial.

Also, consider that different graph systems will potentially have their own methods for more quickly and efficiently removing all data in that system. For example, JanusGraph has this approach:

 JanusGraphFactory.drop(graph)

where "graph" is a JanusGraph instance you want cleared out.

like image 51
stephen mallette Avatar answered Oct 11 '22 21:10

stephen mallette


If you are using Tinkerpop3 (Titan 1.0.0), as said before, the command is:

g.V().drop()

Why this did not work for me

This only works if you are using the Gremlin interactive REPL interface. Why? drop returns an iterator that must be traversed to be applied and the Gremlin REPL interface automatically iterates over returned iterators.

How I fixed it

If (like me) you are using an HTTP or WebSocket interface to Gremlin, you must explicitly iterate over the returned iterator:

g.V().drop().iterate()

Do not forget...

...to commit the transaction. In Titan, transactions are opened implicitly but must be closed explicitly:

g.tx().commit()
like image 21
david_p Avatar answered Oct 11 '22 22:10

david_p