Sunday, 10 May 2026

Creating and Managing Vertexes, Edges with Gremlin Traversals in TinkerPop

  

This post explains the fundamental techniques for creating, modifying, and deleting vertices and edges using Gremlin traversals. These operations form the backbone of graph mutation in Apache TinkerPop and are essential for building and evolving a graph model over time.

 

All examples consistently use the traversal source g, which represents the recommended and portable way to interact with a graph. Direct manipulation of the underlying graph object is intentionally avoided, as traversal based mutations are safer, more expressive, and compatible with remote execution through Gremlin Server.

 

1. Adding Vertices

Vertices represent entities in a graph such as people, systems, locations, or projects. Gremlin provides several flexible ways to create vertices, depending on the use case.

 

Before experimenting with the example, let's create a traversal object.

graph = TinkerGraph.open()
g = graph.traversal()

$gremlin.sh
         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.sugar
plugin activated: tinkerpop.tinkergraph
gremlin> 
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]

   

Basic Vertex Creation with a Label

The simplest form of vertex creation uses the addV() step with a label:

 

g.addV('person')

gremlin> g.addV('person')
==>v[0]

     


 

Adding a Vertex with Properties at Creation Time

In most real-world scenarios, a vertex is created along with descriptive properties. Gremlin supports this through chained property() steps:

g.addV('person').
  property('name', 'Krishna').
  property('age', 29).
  property('role', 'Engineer')

  


  

Each property() step mutates the vertex being created. This pattern keeps vertex definition concise and readable, making the intent of the traversal immediately clear.

gremlin> g.addV('person').
......1>   property('name', 'Krishna').
......2>   property('age', 29).
......3>   property('role', 'Engineer')
==>v[1]

   

We created two vertices now, you can count the total vertices by executing following statement.

 

g.V().count()

gremlin> g.V().count()
==>2

   

Capturing a Newly Created Vertex

Often, a traversal needs to reuse a newly created vertex. For example, when creating edges immediately afterward. Calling next() materializes the vertex and returns it to the client.

 

ram = g.addV('person').
            property('name', 'Ram').
            property('age', 37).
            next()

gremlin> ram = g.addV('person').
......1>             property('name', 'Ram').
......2>             property('age', 37).
......3>             next()
==>v[5]
gremlin>

To inspect the full content of the vertex, start a traversal from the vertex reference and use elementMap() method.  

gremlin> g.V(ram).valueMap()
==>[name:[Ram],age:[37]]

Creating Multiple Vertices in a Single Traversal

Gremlin allows multiple mutation steps within a single traversal.

g.addV('person').property('name', 'Lahari').property('age', '7').
  addV('person').property('name', 'Thulasi').property('age', '5')

   

This style is particularly useful for quickly seeding test data or constructing small example graphs. Each addV() step creates a new vertex independently within the same traversal execution.

 

We can get the both created vertices using within method.

 

g.V().
  hasLabel('person').
  has('name', within('Lahari', 'Thulasi')).
  valueMap(true)

gremlin> g.addV('person').property('name', 'Lahari').property('age', '7').
......1>   addV('person').property('name', 'Thulasi').property('age', '5')
==>v[17]
gremlin>
gremlin> g.V().
......1>   hasLabel('person').
......2>   has('name', within('Lahari', 'Thulasi')).
......3>   valueMap(true)
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:14,label:person,name:[Lahari],age:[7]]

   

Adding Multiple Properties

Multiple properties can be added or updated in a single traversal.

 

g.V().
  hasLabel('person').
  has('name', 'Lahari').
  property('age', 27).
  property('hobbies', 'Trekking')

For example, above statement update age value to 27, and add new property hobbies to Lahari as it is not exists.

 

gremlin> g.V().hasLabel('person').has('name', 'Lahari').valueMap(true)
==>[id:26,label:person,name:[Lahari],age:[7]]
gremlin> 
gremlin> g.V().
......1>   hasLabel('person').
......2>   has('name', 'Lahari').
......3>   property('age', 27).
......4>   property('hobbies', 'Trekking')
==>v[26]
gremlin> 
gremlin> g.V().hasLabel('person').has('name', 'Lahari').valueMap(true)
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]

   

In Gremlin, adding a property with an existing key replaces the old value (for single-valued properties).

 

Updating Based on Conditions

 

g.V().
  hasLabel('person').
  has('age', gt(25)).
  property('discount', 10)

   

This updates only vertices matching the condition, Add or update the property discount to 10 for the person age greater than 25.

 

gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]
gremlin> 
gremlin> g.V().
......1>   hasLabel('person').
......2>   has('age', gt(25)).
......3>   property('discount', 10)
==>v[1]
==>v[5]
==>v[26]
gremlin> 
gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,role:[Engineer],name:[Krishna],discount:[10],age:[29]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],discount:[10],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]]

Deleting Vertex Properties

To remove a property, use properties() followed by drop().

g.V().
  hasLabel('person').
  has('name', 'Krishna').
  properties('discount').
  drop()

Above statement removes the property 'discount' to the person 'Krishna'.

gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,role:[Engineer],name:[Krishna],discount:[10],age:[29]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],discount:[10],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]]
gremlin> 
gremlin> 
gremlin> g.V().
......1>   hasLabel('person').
......2>   has('name', 'Krishna').
......3>   properties('discount').
......4>   drop()
gremlin> 
gremlin> 
gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],discount:[10],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]]

   

Following statement removes the discount property from all person vertices.

 

g.V().
  hasLabel('person').
  properties('discount').
  drop()

gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],discount:[10],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],discount:[10],age:[27]]
gremlin> 
gremlin> g.V().
......1>   hasLabel('person').
......2>   properties('discount').
......3>   drop()
gremlin> 
gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]

   

Deleting Multiple Properties

 

g.V().
  hasLabel('person').
  has('name', 'Krishna').
  properties('role', 'age').
  drop()

   

Above statement drop role and age properties for the person named 'Krishna'.

 

gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,role:[Engineer],name:[Krishna],age:[29]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]
gremlin> 
gremlin> 
gremlin> g.V().
......1>   hasLabel('person').
......2>   has('name', 'Krishna').
......3>   properties('role', 'age').
......4>   drop()
gremlin> 
gremlin> 
gremlin> g.V().hasLabel('person').valueMap(true)
==>[id:0,label:person]
==>[id:1,label:person,name:[Krishna]]
==>[id:17,label:person,name:[Thulasi],age:[5]]
==>[id:5,label:person,name:[Ram],age:[37]]
==>[id:26,label:person,hobbies:[Trekking],name:[Lahari],age:[27]]

   

2. Adding Edges

Edges represent relationships between vertices.

 

Adding an Edge Using to()

Let's create a project 'Jarvis'.

 

g.addV('project').property('name', 'Jarvis').next()

gremlin> g.addV('project').property('name', 'Jarvis').next()
==>v[0]

Let's create two person 'Abhishek', 'Narasimha'

gremlin> g.addV('person').property('name', 'Abhishek').next()
==>v[2]
gremlin> g.addV('person').property('name', 'Narasimha').next()
==>v[4]

Let's print the vertices with names Jarvis, Abhishek and Narasimha.

gremlin> g.V().has('name', within('Jarvis', 'Abhishek', 'Narasimha')).valueMap(true)
==>[id:0,label:project,name:[Jarvis]]
==>[id:2,label:person,name:[Abhishek]]
==>[id:4,label:person,name:[Narasimha]]

   

Following statements adds and edge 'works_on' from Abhishek to Jarvis.

 

abhishek = g.V().hasLabel('person').has('name', 'Abhishek').next();[]
jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[]
g.V(abhishek).addE('works_on').to(jarvis)

gremlin> abhishek = g.V().hasLabel('person').has('name', 'Abhishek').next();[]
gremlin> jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[]
gremlin> 
gremlin> g.V(abhishek).addE('works_on').to(jarvis)
==>e[6][2-works_on->0]

   

Get all the persons who worked on the project 'jarvis'.

 

g.V(jarvis).
 in('works_on').
 valueMap(true)

gremlin> g.V(jarvis).
......1>  in('works_on').
......2>  valueMap(true)
==>[id:2,label:person,name:[Abhishek]]

Adding an Edge Using from()

g.V(jarvis).
 addE('manages').
 from(narasimha)

narasimha = g.V().hasLabel('person').has('name', 'Narasimha').next();[]
jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[]

gremlin> narasimha = g.V().hasLabel('person').has('name', 'Narasimha').next();[]
gremlin> jarvis = g.V().hasLabel('project').has('name', 'Jarvis').next();[]
gremlin> 
gremlin> g.V(jarvis).
......1>  addE('manages').
......2>  from(narasimha)
==>e[7][4-manages->0]

Get the person who manages the project 'jarvis'

gremlin> g.V(jarvis).in('manages').valueMap(true)
==>[id:4,label:person,name:[Narasimha]]

   

Get the projects managed by Narasimha.

 

gremlin> g.V(narasimha).out('manages').valueMap(true)
==>[id:0,label:project,name:[Jarvis]]

   

Adding an Edge Using as() Labels

The as() step labels a vertex for later reference within the traversal.

 

joel = g.addV('person').property('name', 'Joel').next()
alice = g.addV('project').property('name', 'alice').next()

g.V().hasLabel('person').has('name', 'Joel').as('p').
  V().hasLabel('project').has('name', 'alice').
  addE('manages').from('p')

   

Here, above statements model the relation ship joel -> manages -> alice. The as() step labels a vertex for later reference within the traversal.

 

gremlin> joel = g.addV('person').property('name', 'Joel').next()
==>v[8]
gremlin> 
gremlin> alice = g.addV('project').property('name', 'alice').next()
==>v[10]
gremlin> 
gremlin> g.V().hasLabel('person').has('name', 'Joel').as('p').
......1>   V().hasLabel('project').has('name', 'alice').
......2>   addE('manages').from('p')
==>e[12][8-manages->10]

   

Adding Properties to an existing Edge

Edges can have properties just like vertices.

 

Get the Edge:

 

my_edge = g.V().has('person', 'name', 'Joel').outE('manages').next()
g.E(my_edge).valueMap(true)

gremlin> my_edge = g.V().has('person', 'name', 'Joel').outE('manages').next()
==>e[12][8-manages->10]
gremlin> 
gremlin> 
gremlin> g.E(my_edge).valueMap(true)
==>[id:12,label:manages]

   

Attach properties to the edge using property method.

 

g.E(my_edge).
 property('since', 2026).
 property('allocation', 35)

gremlin> g.E(my_edge).
......1>  property('since', 2026).
......2>  property('allocation', 35)
==>e[12][8-manages->10]
gremlin> 
gremlin> g.E(my_edge).valueMap(true)
==>[id:12,label:manages,allocation:35,since:2026]

   

Adding Properties to an Edge At the time of Creation

You can add properties to an edge at the time of creation as well.

 

gopi = g.addV('person').
  property('name', 'Gopi').
  property('age', 41).
  next()

galelio = g.addV('project').
  property('name', 'Galelio').
  next()

g.V(gopi).
  addE('works_on').
  to(galelio).
  property('since', 2024).
  property('employee_type', 'permanent')

Above statement add the properties 'since', 'employee_type' to the relation 'works_on'.  

gremlin> gopi = g.addV('person').
......1>   property('name', 'Gopi').
......2>   property('age', 41).
......3>   next()
==>v[13]
gremlin> 
gremlin> galelio = g.addV('project').
......1>   property('name', 'Galelio').
......2>   next()
==>v[16]
gremlin> 
gremlin> g.V(gopi).
......1>   addE('works_on').
......2>   to(galelio).
......3>   property('since', 2024).
......4>   property('employee_type', 'permanent')
==>e[20][13-works_on->16]
gremlin> 
gremlin> 
gremlin> g.V(gopi).outE('works_on').valueMap(true)
==>[id:20,label:works_on,employee_type:permanent,since:2024]

   

Updating an Edge Property

 

g.E().
 hasLabel('manages').
 has('allocation', 35).
 property('since', 2021)

   

Above statement updates the property 'since' to the value 2021

 

gremlin> g.E().valueMap(true)
==>[id:20,label:works_on,employee_type:permanent,since:2024]
==>[id:6,label:works_on]
==>[id:7,label:manages]
==>[id:12,label:manages,allocation:35,since:2026]
gremlin>
gremlin>
gremlin> g.E().
......1>  hasLabel('manages').
......2>  has('allocation', 35).
......3>  property('since', 2021)
==>e[12][8-manages->10]
gremlin> 
gremlin> 
gremlin> g.E().valueMap(true)
==>[id:20,label:works_on,employee_type:permanent,since:2024]
==>[id:6,label:works_on]
==>[id:7,label:manages]
==>[id:12,label:manages,allocation:35,since:2021]

   

Deleting an Edge Properties

 

g.E().
 properties('since').
 drop()

   

Above snippet drops the property 'since' from all the edges.

 

gremlin> g.E().valueMap(true)
==>[id:20,label:works_on,employee_type:permanent,since:2024]
==>[id:6,label:works_on]
==>[id:7,label:manages]
==>[id:12,label:manages,allocation:35,since:2021]
gremlin> 
gremlin> g.E().
......1>  properties('since').
......2>  drop()
gremlin> 
gremlin> g.E().valueMap(true)
==>[id:20,label:works_on,employee_type:permanent]
==>[id:6,label:works_on]
==>[id:7,label:manages]
==>[id:12,label:manages,allocation:35]

   

Deleting the Entire Edge

 

g.E().hasLabel('works_on').drop()

   

Above statement drop all the edges with label 'works_on'.

 

gremlin> g.E().valueMap(true)
==>[id:20,label:works_on,employee_type:permanent]
==>[id:6,label:works_on]
==>[id:7,label:manages]
==>[id:12,label:manages,allocation:35]
gremlin> 
gremlin> g.E().hasLabel('works_on').drop()
gremlin> 
gremlin> g.E().valueMap(true)
==>[id:7,label:manages]
==>[id:12,label:manages,allocation:35]

   

Note:

·      Always prefer g.addV() and g.addE() over direct graph mutations

·      property() is used for create, update, and overwrite

·      drop() removes properties, edges, or vertices

·      as(), to(), and from() enable expressive, readable traversals

·      Traversal based mutations are portable, safe, and Gremlin Server–friendly


 

 

Previous                                                    Next                                                    Home

Turning Off Unnecessary Output in the Gremlin Console

   

When working interactively in the Gremlin Console, it is common to experiment with traversals, assign results to variables, and iteratively refine queries. In many such situations, the result itself is important, but the console output is not.

 

By default, the Gremlin Console eagerly prints the result of every executed statement. While this behavior is helpful during exploration and learning, it can become distracting when:

 

·      Assigning traversal results to variables

·      Executing traversals with large result sets

·      Running preparatory or intermediate steps whose output is not immediately relevant

·      Benchmarking or profiling traversals where console I/O adds noise

 

In these cases, suppressing console output improves clarity and keeps the session focused.

 

A Simple Technique to Suppress Output

The Gremlin Console evaluates and prints the result of the last expression in a line. This behavior can be leveraged to intentionally return a harmless value instead of the traversal result.

 

Appending an empty list expression (;[]) at the end of a statement ensures that the console prints an empty list, rather than the potentially large or verbose traversal output.

 

Example

gremlin> persons = g.V().hasLabel('person').toList()
==>v[0]
==>v[3]
==>v[6]
gremlin> 
gremlin> persons
==>v[0]
==>v[3]
==>v[6]

We can append [] to the statement so the console prints empty list post evaluation.

persons = g.V().hasLabel('person').toList();[]

gremlin> persons = g.V().hasLabel('person').toList();[]
gremlin> persons
==>v[0]
==>v[3]
==>v[6]

What Happens Here?

·      The traversal executes fully and assigns its result to the variable persons.

·      The semicolon (;) separates statements.

·      The final expression evaluated by the console is [] (an empty list).

·      As a result, the console prints [] instead of the traversal output.

 

The traversal still runs exactly as written, and the variable assignment is preserved.

 

Why This Works?

The Gremlin Console follows standard Groovy semantics:

 

·      Multiple statements can appear on a single line.

·      Only the result of the last evaluated expression is displayed.

·      Earlier expressions are executed, but their results are not printed unless explicitly returned.

 

In summary, suppressing Gremlin Console output does not require special flags or configuration. By appending an empty list expression to a statement, execution proceeds normally while console output remains clean and focused. This small technique can significantly improve the usability of interactive Gremlin sessions, especially as traversal complexity and result size grow.

  

  

Previous                                                    Next                                                    Home

Thursday, 7 May 2026

Loading Scripts in the Gremlin Console with the :load Command

      The Gremlin Console is designed to support exploratory graph work, experimentation, and iterative learning. As graphs and console configurations become more involved, repeatedly typing setup commands quickly becomes inefficient and error-prone. The :load command addresses this problem by allowing an entire Groovy script to be executed inside the console with a single instruction.

 

At its core, :load enables repeatable environment initialization. Rather than treating the console as a blank slate on every restart, you can externalize graph creation, configuration, and data loading into a reusable script file and apply it consistently whenever the console starts.

 

What the :load Command Does

The :load command instructs the Gremlin Console to read a Groovy file from disk and execute it line by line, as though each statement were typed directly into the console.

 

Key characteristics include:

·      The file is executed in the current console session

·      All variables defined in the script become available interactively

·      Errors are surfaced immediately, just as they would be with manual input

·      The command can be invoked multiple times during a session

 

This makes :load especially suitable for initializing graphs, defining traversal sources, importing data, and enabling console options.

 

Applying :load to Graph Initialization

Suppose I have following groovy script.

 

sample-graph.groovy

graph = TinkerGraph.open()
g = graph.traversal()

// People
alice = g.addV('person').property('name', 'Alice').property('role', 'Engineer').next()
bob = g.addV('person').property('name', 'Bob').property('role', 'Engineer').next()
carol = g.addV('person').property('name', 'Carol').property('role', 'Manager').next()

// Projects
apollo = g.addV('project').property('name', 'Apollo').property('status', 'Active').next()
zeus = g.addV('project').property('name', 'Zeus').property('status', 'Completed').next()


// People working on projects
g.addE('works_on').from(alice).to(apollo).property('since', 2022)
g.addE('works_on').from(alice).to(zeus).property('since', 2021)

g.addE('works_on').from(bob).to(apollo).property('since', 2023)
g.addE('works_on').from(bob).to(zeus).property('since', 2022)

// Management relationships
g.addE('manages').from(carol).to(apollo)

// Collaboration between people
g.addE('collaborates_with').from(alice).to(bob)
g.addE('collaborates_with').from(bob).to(alice)

// Reporting structure
g.addE('reports_to').from(alice).to(carol)
g.addE('reports_to').from(bob).to(carol)

// Oversight
g.addE('oversees').from(carol).to(zeus)

   

We can load this groovy script into Gremlin console by executing below statement.

:load /Users/Shared/graph/sample-graph.groovy

gremlin> :load /Users/Shared/graph/sample-graph.groovy
==>tinkergraph[vertices:0 edges:0]
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
==>true
==>v[0]
==>v[3]
==>v[6]
==>true
==>v[9]
==>v[12]
==>true
==>e[15][0-works_on->9]
==>e[16][0-works_on->12]
==>e[17][3-works_on->9]
==>e[18][3-works_on->12]
==>true
==>e[19][6-manages->9]
==>true
==>e[20][0-collaborates_with->3]
==>e[21][3-collaborates_with->0]
==>true
==>e[22][0-reports_to->6]
==>e[23][3-reports_to->6]
==>true
==>e[24][6-oversees->12]
gremlin>

   

Since the groovy script is loaded completely, we can start querying the Graph.

 

gremlin> g.V().valueMap(true)
==>[id:0,label:person,role:[Engineer],name:[Alice]]
==>[id:3,label:person,role:[Engineer],name:[Bob]]
==>[id:6,label:person,role:[Manager],name:[Carol]]
==>[id:9,label:project,name:[Apollo],status:[Active]]
==>[id:12,label:project,name:[Zeus],status:[Completed]]

Why Script Based Loading Matters?

Using :load is not merely a convenience; it is a best practice for serious Gremlin work.

 

·      Consistency: A scripted setup ensures the same graph configuration is used every time. This is particularly important when examples rely on specific ID types, traversal sources, or graph features.

·      Speed and Focus: With setup automated, attention can be directed immediately toward traversal design and graph exploration rather than environment preparation.

·      Reduced Cognitive Load: Complex configuration details—such as ID manager settings—are captured once and reused, eliminating the need to remember or retype them accurately.

·      Reproducibility: Scripts can be shared across teams, books, or training materials, ensuring that readers and practitioners start from an identical baseline.

 

File Location and Path Considerations

The :load command resolves file paths relative to the console’s current working directory. For this reason, using absolute paths is strongly recommended, particularly when scripts reference external resources such as GraphML files.

 

Example

 

:load /full/path/to/sample-graph.groovy

   

This avoids ambiguity and prevents file-not-found errors that can arise when directory contexts change between sessions or systems.

 

In summary, the :load command plays a role similar to a bootstrap script in other development environments. It establishes the foundation upon which all subsequent work depends. By separating environment setup from interactive querying, it promotes clarity, discipline, and reproducibility qualities that align well with professional graph development practices.

 

Previous                                                    Next                                                    Home