35

I know how to remove a vertex by id in Gremlin. But now I'm need to cleanup the database. How do I delete multiple vertices?

Deleting 1 v is like this:

    ver = g.v(1)
    g.removeVertex(ver)

I mean something like SQL TRUNCATE. How do you remove the vertices / vertexes without removing the class?

Zach
  • 539
  • 1
  • 4
  • 22
Aleksandrenko
  • 2,987
  • 6
  • 28
  • 34

8 Answers8

60

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.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • 1
    How would you do this for large graphs? I tried this with a graph with around 200k vertices and it was painfully slow. – ThePhysicist Aug 25 '14 at 15:27
  • 4
    Faster methods exist, but may be dependent on the graph database itself (like, if you were using Titan you could probably use the `TitanCleanup` utility - http://thinkaurelius.github.io/titan/javadoc/0.5.0/com/thinkaurelius/titan/core/util/TitanCleanup.html). There is no "Gremlin" way to remove all vertices faster. – stephen mallette Aug 25 '14 at 17:38
  • 1
    For those using JanusGraph you would want the JanusGraphCleanup utility -https://github.com/JanusGraph/janusgraph/blob/dbbda285af0b3752b75178aa4dd62516200c50d5/janusgraph-core/src/main/java/org/janusgraph/core/util/JanusGraphCleanup.java – stephen mallette Jul 19 '17 at 17:53
35

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()
david_p
  • 5,722
  • 1
  • 32
  • 26
22

you can try

g.V.each{g.removeVertex(it)}
g.commit()
Ruben Trancoso
  • 1,444
  • 5
  • 27
  • 55
Peter Neubauer
  • 6,311
  • 1
  • 21
  • 24
8

In TinkerPop3:

The drop()-step (filter/sideEffect) is used to remove element and properties from the graph (i.e. remove).

g.V().drop()
Daishi Nakajima
  • 1,932
  • 18
  • 26
  • This is what works on AWS Neptune. It's Incredible so many people reposted this answer and this one appears at the bottom. I traditionally look for the first replier to give my vote accordingly. – Wilmer E. Henao May 27 '21 at 12:39
7

You can do it as follows;

graph.shutdown();
TitanCleanup.clear(graph);
Ducaz035
  • 3,054
  • 2
  • 25
  • 45
  • Although the OP asked for a gremlin answer, this works in Java by passing in a com.thinkaurelius.titan.core.TitanGraph as 'graph' in the example above. – PHY6 Aug 15 '14 at 02:47
5

Blueprints used to have a clear() method for this...

g.clear()

But it was recently removed:

https://github.com/tinkerpop/blueprints/issues/248

espeed
  • 4,754
  • 2
  • 39
  • 51
3

In TinkerPop3, with Titan-1.0.0,

g.V().drop()
g.tx().commit()   (commit the changes)

works for me. You can give a try

0
public class JanusGraphCleanup {
    @Deprecated
    public static void clear(JanusGraph graph) throws BackendException {
        JanusGraphFactory.drop(graph);
    }
}

Ref: https://github.com/JanusGraph/janusgraph/blob/master/janusgraph-core/src/main/java/org/janusgraph/core/util/JanusGraphCleanup.java

arctic_Oak
  • 974
  • 2
  • 13
  • 29