DROP GRAPH#

Removes a graph, and with CASCADE every label, vertex and edge in it.

Synopsis#

DROP GRAPH [ IF EXISTS ] graph_name [, ... ] [ CASCADE | RESTRICT ]

bold keywords · italic placeholders you supply · [ ] optional · ... repeatable. See Syntax conventions.

Description#

DROP GRAPH drops the graph’s catalog row and its schema. Everything the graph holds depends on that schema: labels, the tables behind them, their rows, indexes, constraints and sequences. So RESTRICT, which is the default, refuses while any of it is there.

A graph made by the plain CREATE GRAPH is never empty: ag_vertex, ag_edge and ag_label_seq are created with it. So every such graph needs CASCADE. Only a graph made with CREATE GRAPH ONLY, which creates none of those, can be dropped without it.

  • Dropping a graph does not require its labels to be empty. CASCADE takes the rows with the tables.

  • If the dropped graph is the session’s graph_path, the setting is cleared.

  • The graph’s schema cannot be dropped on its own: DROP SCHEMA on it is refused and points you here.

Parameters#

IF EXISTS

Issue a notice instead of an error for a graph that does not exist. Other names in the same statement are still dropped.

graph_name

Name of the graph to drop. Several names may be given, separated by commas.

CASCADE

Drop the objects that depend on the graph: its labels, their tables and rows, and the label-id sequence.

RESTRICT

Refuse if anything depends on the graph. This is the default.

Examples#

Drop a graph and everything in it#

CREATE GRAPH clearance;
SET graph_path = clearance;
CREATE VLABEL film;
CREATE (:film {title: 'Arrival'});

Without CASCADE the drop is refused, and the message lists what stands in the way:

DROP GRAPH clearance;
ERROR:  cannot drop graph clearance because other objects depend on it
DETAIL:  sequence clearance.ag_label_seq depends on schema clearance
vlabel ag_vertex depends on schema clearance
elabel ag_edge depends on schema clearance
vlabel film depends on schema clearance
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

CASCADE takes all four, and the vertex in film with them:

DROP GRAPH clearance CASCADE;
NOTICE:  drop cascades to 4 other objects
DETAIL:  drop cascades to sequence clearance.ag_label_seq
drop cascades to vlabel ag_vertex
drop cascades to elabel ag_edge
drop cascades to vlabel film
DROP GRAPH

clearance was the session’s graph_path, so the setting is now empty:

SHOW graph_path;
 graph_path
------------

(1 row)

Drop several graphs, skipping the ones that are gone#

CREATE GRAPH archive_2024;
CREATE GRAPH archive_2025;
DROP GRAPH IF EXISTS archive_2024, archive_2025, archive_2026 CASCADE;
NOTICE:  graph "archive_2026" does not exist, skipping
NOTICE:  drop cascades to 6 other objects
DETAIL:  drop cascades to sequence archive_2025.ag_label_seq
drop cascades to vlabel ag_vertex
drop cascades to elabel ag_edge
drop cascades to sequence archive_2024.ag_label_seq
drop cascades to vlabel ag_vertex
drop cascades to elabel ag_edge
DROP GRAPH

The missing name is reported and the two that exist are dropped:

\dG archive*
No matching graphs found.

Notes#

To empty a graph without dropping it, run MATCH (n) DETACH DELETE n and then drop the labels you no longer want. The schema and the base labels stay in place. A label has to be empty before DROP VLABEL will remove it, so that delete is the usual first step. DROP GRAPH ... CASCADE skips the requirement altogether, because it drops the tables rather than the labels one at a time.

Compatibility#

ISO/IEC 39075 (GQL)#

GQL’s <drop graph statement> (§12.5) is DROP [PROPERTY] GRAPH [IF EXISTS] <name>. AgensGraph accepts the DROP GRAPH and IF EXISTS parts of that. It adds a name list and the CASCADE/RESTRICT choice, which GQL does not have. The PROPERTY spelling is not accepted: DROP PROPERTY GRAPH clearance is a syntax error.

PostgreSQL#

DROP GRAPH corresponds to DROP SCHEMA, and IF EXISTS, the name list and CASCADE/RESTRICT behave as they do there. It differs in refusing to leave a half-dropped graph behind. DROP SCHEMA on a graph’s schema fails with cannot drop schema clearance because graph clearance requires it, so the catalog row and the schema always go together.

See also#