Before the answer: run the inspection query in the next section. Every method on this page is irreversible without a restore, and the single most common way this goes wrong is not a bad command — it is a correct command run against the wrong database. Then: DROP SCHEMA public CASCADE; CREATE SCHEMA public; is the fastest way to empty a database, and on PostgreSQL 15 and later it leaves the schema with fewer permissions than it started with, which is the part nobody warns you about. If you want the tables to survive and only the rows to go, you want TRUNCATE ... CASCADE, not DROP.
Table of Contents
Step 0: prove what you are about to destroy
Non-destructive, takes a second, and tells you whether you are pointed at the database you think:
SELECT current_database(), current_user, inet_server_port();
SELECT n.nspname AS schema,
count(*) FILTER (WHERE c.relkind IN ('r','p')) AS tables,
count(*) FILTER (WHERE c.relkind = 'v') AS views,
count(*) FILTER (WHERE c.relkind = 'S') AS sequences,
pg_size_pretty(sum(pg_total_relation_size(c.oid))) AS total
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname NOT IN ('pg_catalog','information_schema') AND n.nspname !~ '^pg_'
GROUP BY 1 ORDER BY 1;
schema | tables | views | sequences | total
--------+--------+-------+-----------+--------
app | 2 | 0 | 2 | 344 kB
public | 7 | 1 | 2 | 584 kB
(2 rows)
Two schemas, not one. Every “delete all tables” snippet on the internet filters on schemaname = 'public' and would silently leave app untouched. Follow it with the row counts:
SELECT schemaname||'.'||relname AS table, n_live_tup AS est_rows
FROM pg_stat_user_tables WHERE n_live_tup > 0 ORDER BY n_live_tup DESC;
table | est_rows
---------------------+----------
public.orders | 5000
app.sessions | 2000
Take a dump first — pg_dump -Fc -d yourdb -f pre-wipe.dump — and confirm the file is non-zero before you continue. See backing up with pg_dump.
Method 1: drop and recreate the schema
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT USAGE ON SCHEMA public TO PUBLIC;
ALTER SCHEMA public OWNER TO pg_database_owner;
Those last two lines are not optional on PostgreSQL 15+. The public schema that initdb ships is owned by pg_database_owner and grants USAGE to PUBLIC:
hwacl=# \dn+ public
Name | Owner | Access privileges
--------+-------------------+----------------------------------------
public | pg_database_owner | pg_database_owner=UC/pg_database_owner+
| | =U/pg_database_owner
A schema you create yourself gets neither. Here is one created with a plain CREATE SCHEMA, exactly as the recovery step above would leave it:
hwacl=# \dn+ public_lookalike
Name | Owner | Access privileges
------------------+----------+-------------------
public_lookalike | postgres |
Empty access privileges means owner-only — PUBLIC has lost even USAGE, so a table grant that has visibly been issued does nothing: the role cannot enter the schema to reach the table. Identical GRANT SELECT, two schemas:
hwacl=> SELECT * FROM public.t;
i
---
1
(1 row)
hwacl=> SELECT * FROM public_lookalike.t;
ERROR: permission denied for schema public_lookalike
LINE 1: SELECT * FROM public_lookalike.t;
^
Running the two repair statements restores the shipped access privileges byte for byte, and the select starts working:
hwacl=# GRANT USAGE ON SCHEMA public_lookalike TO PUBLIC;
hwacl=# ALTER SCHEMA public_lookalike OWNER TO pg_database_owner;
hwacl=# \dn+ public_lookalike
Name | Owner | Access privileges
------------------+-------------------+----------------------------------------
public_lookalike | pg_database_owner | pg_database_owner=UC/pg_database_owner+
| | =U/pg_database_owner
Before PostgreSQL 15 the shipped public schema granted CREATE to PUBLIC as well, so every role could make tables in it. That grant was removed in 15 and is not coming back; do not add it to “fix” an application. Grant CREATE to the specific role that needs it.
Method 2: generate the DROP statements and read them first
The statements below are generated, printed, and deliberately not executed here. What follows proves the generator is correct and hands you the output to run yourself.
SELECT format('DROP TABLE IF EXISTS %I.%I CASCADE;', schemaname, tablename)
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog','information_schema')
ORDER BY 1;
The format('%I') matters. Building the statement as 'DROP TABLE ' || tablename, bare, produces text that is not the statement you meant on a database with any table whose name is quoted, mixed-case, or a reserved word:
hwgen=# SELECT tablename AS bare_concat,
hwgen-# format('%I', tablename) AS format_i,
hwgen-# format('%I.%I', schemaname, tablename) AS schema_qualified
hwgen-# FROM pg_tables WHERE schemaname='public' ORDER BY 1;
bare_concat | format_i | schema_qualified
--------------+---------------+----------------------
Order Items | "Order Items" | public."Order Items"
plain_orders | plain_orders | public.plain_orders
user | "user" | public."user"
Feed the bare forms back to the parser and you get a syntax error on one and, worse, a silent misparse on the other — user is an SQL keyword:
hwgen=# SELECT * FROM Order Items;
ERROR: syntax error at or near "Order"
hwgen=# SELECT * FROM user;
user
----------
postgres
(1 row)
The schema qualification matters for the same reason. Two tables can share a bare name, and which one an unqualified statement hits depends on search_path at the moment it runs:
search_path=staging -> staging.plain_orders
search_path=public -> public.plain_orders
Generate, read the output, then paste it back. Do not wrap it in a DO $$ ... EXECUTE $$ block — that runs the whole thing unseen, which removes the only review step you had.
Method 3: TRUNCATE, when you want the schema to survive
TRUNCATE TABLE public.orders, public.customers RESTART IDENTITY CASCADE;
This empties tables without dropping them, so views, functions, grants, sequences and foreign keys all survive. Build the list the same way:
SELECT string_agg(format('%I.%I', schemaname, tablename), ', ')
FROM pg_tables WHERE schemaname NOT IN ('pg_catalog','information_schema');
CASCADE does not mean here what it means in DROP: it truncates every table with a foreign key pointing at the ones you named, so it can empty tables you did not list — and without it, truncating a referenced table fails outright. Run the dependency query first:
SELECT conrelid::regclass AS child, confrelid::regclass AS parent, conname
FROM pg_constraint WHERE contype = 'f';
TRUNCATE is transactional in PostgreSQL, unlike MySQL — wrap it in BEGIN; ... ROLLBACK; to rehearse. That rehearsal is not available for DROP DATABASE, which cannot run inside a transaction block at all.
I did not execute TRUNCATE or the foreign-key-failure case. The behaviour above is as documented in the PostgreSQL 17 TRUNCATE reference; treat it as cited rather than measured, unlike the rest of this page. With HW_ALLOW_DESTRUCTIVE=1 set, how-to-delete-all-tables.sh runs those two cases and the DROP SCHEMA one against a cluster of its own, never an existing installation; without the variable these checks print SKIP and the others still run.
If you actually wanted the database gone
DROP DATABASE is one statement and no loose ends, but it cannot run while anyone is connected to the target — including you. Connect to postgres instead, or use dropdb from the shell, and on PostgreSQL 13+ add WITH (FORCE) to evict existing sessions.
Verified 2026-08-10 on PostgreSQL 17.10 (Homebrew), aarch64-apple-darwin25.6.0. The inspection queries, the schema access-privilege comparison, the permission-denied error and the identifier-quoting results were executed on a throwaway cluster and pasted unedited. The DROP and TRUNCATE statements were generated but deliberately not run, as stated in Method 2.