13.4 Deleting a Graph
In order to reduce the memory usage of the graph server (PGX), the session must drop the unused PgxGraph
graph objects that it created via PgxSession.getGraph()
by invoking the destroy()
method. This step not only destroys the specified graph, but all of its associated properties, including transient properties as well. In addition, all of the collections related to the graph instance (for example, a VertexSet
) are also destroyed automatically. If a session holds multiple PgxGraph
objects referencing the same graph, invoking destroy()
on any of them will invalidate all the PgxGraph
objects referencing that graph, making any operation on those objects fail:
PgxGraph graph1 = session.getGraph("myGraphName") // graph2 references the same graph of graph1 PgxGraph graph2 = session.getGraph("myGraphName") // both calls throw an exception, as both references are not valid anymore Set<VertexProperty<?, ?>> properties = graph1.getVertexProperties(); properties = graph2.getVertexProperties()
graph1 = session.get_graph("myGraphName") # graph2 references the same graph of graph1 graph2 = session.get_graph("myGraphName") # both calls throw an exception, as both references are not valid anymore properties = graph1.get_vertex_properties() properties = graph2.get_vertex_properties()
The same behavior occurs when multiple PgxGraph
objects reference the same snapshot. Since a snapshots is effectively a graph, destroying a PgxGraph
object referencing a certain snapshot invalidates all PgxGraph
objects referencing the same snapshot, but does not invalidate those referencing other snapshots:
// get a snapshot of "myGraphName"
PgxGraph graph1 = session.getGraph("myGraphName");
// graph2 and graph3 reference the same snapshot as graph1
PgxGraph graph2 = session.getGraph("myGraphName");
PgxGraph graph3 = session.getGraph("myGraphName");
// we assume another snapshot is created ...
// make graph3 references the latest snapshot available
session.setSnapshot(graph3, PgxSession.LATEST_SNAPSHOT);
graph2.destroy();
// both calls throw an exception, as both references are not valid anymore
Set<VertexProperty<?, ?>> properties = graph1.getVertexProperties();
properties = graph2.getVertexProperties();
// graph3 is still valid, so the call succeeds
properties = graph3.getVertexProperties();
Note:
Even if a graph is destroyed by a session, the graph data may still remain in the server memory if the graph is currently shared by other sessions. In such a case, the graph may still be visible among the available graphs via PgxSession.getGraphs()
.
As a safe alternative to manual destruction of each graph, the PGX API supports some implicit resource management features which allow developers to safely omit the destroy()
call. See Resource Management Considerations for more information.
Parent topic: Graphs Management