DROP
The DROP statement removes a catalog entry added previously with the CREATE command.
Examples
Delete the table with the name tbl:
DROP TABLE tbl;Drop the view with the name view1; do not throw an error if the view does not exist:
DROP VIEW IF EXISTS view1;Drop function fn:
DROP FUNCTION fn;Drop index idx:
DROP INDEX idx;Drop schema sch:
DROP SCHEMA sch;Drop sequence seq:
DROP SEQUENCE seq;Drop macro mcr:
DROP MACRO mcr;Drop macro table mt:
DROP MACRO TABLE mt;Drop type typ:
DROP TYPE typ;Drop text search dictionary dict:
DROP TEXT SEARCH DICTIONARY dict;Syntax
Dependencies of Dropped Objects
SereneDB performs limited dependency tracking for some object types.
By default or if the RESTRICT clause is provided, the entry will not be dropped if there are any other objects that depend on it.
If the CASCADE clause is provided then all the objects that are dependent on the object will be dropped as well.
CREATE SCHEMA myschema;
CREATE TABLE myschema.t1 (i INTEGER);
DROP SCHEMA myschema;error cannot drop schema myschema because other objects depend on it
The CASCADE modifier drops both myschema and myschema.t1:
CREATE SCHEMA myschema;
CREATE TABLE myschema.t1 (i INTEGER);
DROP SCHEMA myschema CASCADE;The following dependencies are tracked and thus will raise an error if the user tries to drop the depending object without the CASCADE modifier.
| Depending object type | Dependent object type |
|---|---|
SCHEMA | FUNCTION |
SCHEMA | MACRO TABLE |
SCHEMA | MACRO |
SCHEMA | SCHEMA |
SCHEMA | SEQUENCE |
SCHEMA | TABLE |
SCHEMA | TYPE |
SCHEMA | VIEW |
TABLE | VIEW |
TEXT SEARCH DICTIONARY | INDEX |
Dependencies on Views
Views that reference a table are tracked as dependents of that table. If a view references a table and the table is dropped with RESTRICT (the default), then the drop is rejected and the view stays valid:
CREATE TABLE tbl (i INTEGER);
CREATE VIEW view1 AS SELECT i FROM tbl;
DROP TABLE tbl RESTRICT;
SELECT * FROM view1;db error: ERROR: cannot drop table tbl because other objects depend on it DETAIL: view view1 depends on table tbl HINT: Use DROP ... CASCADE to drop the dependent objects too. i
Dependencies across Databases
A view or macro may read objects of another database, and a table may take a column default (nextval) or a column type from one. Those references are tracked the same way: dropping the referenced object with RESTRICT is rejected, and so is DROP DATABASE while an object of another database depends on one of its objects. CASCADE also drops a dependent view or macro in the other database; a dependent table stays, and a column default that used a dropped sequence is removed:
CREATE DATABASE sales;
CREATE TABLE sales.public.orders (id INTEGER, amount INTEGER);
CREATE VIEW sales_report AS SELECT sum(amount) AS total FROM sales.public.orders;
DROP TABLE sales.public.orders;
DROP DATABASE sales;
DROP TABLE sales.public.orders CASCADE;
DROP DATABASE sales;db error: ERROR: cannot drop table orders because other objects depend on it DETAIL: view sales_report depends on table orders HINT: Use DROP ... CASCADE to drop the dependent objects too. db error: ERROR: cannot drop database sales because other objects depend on it DETAIL: view sales_report depends on database sales HINT: Use DROP ... CASCADE to drop the dependent objects too.
DROP DATABASE ... CASCADE drops the dependents in other databases together with the database:
CREATE DATABASE sales;
CREATE TABLE sales.public.orders (id INTEGER, amount INTEGER);
CREATE VIEW sales_report AS SELECT sum(amount) AS total FROM sales.public.orders;
DROP DATABASE sales CASCADE;
SELECT count(*) AS reports FROM information_schema.views WHERE table_name = 'sales_report'; reports
---------
0The same holds between concurrent transactions: when one transaction drops an object and another adds a reference to it from another database, whichever commits second fails at COMMIT and changes nothing.
Reclaiming Disk Space
DROP TABLE, DROP INDEX and DROP DATABASE remove the files of the dropped objects once the dropping transaction commits and no running query reads them; while a backup runs, the files stay until pg_backup_stop. PRAGMA database_size reports, for each database, the size of its tables and indexes in database_size:
PRAGMA database_size;In an attached DuckDB database file, DROP TABLE marks the table's blocks as free without shrinking the file, and PRAGMA database_size counts them in free_blocks. To reclaim that space, use the CHECKPOINT statement or create a fresh copy with the COPY FROM DATABASE statement.