Okay, here is a SQL script using PL/pgSQL that will dynamically find and drop all tables in your PostgreSQL database, excluding system schemas and common Supabase internal schemas. It uses CASCADE to handle foreign key dependencies.
!!! WARNING !!!
This script is destructive and irreversible. It will delete ALL tables (except those in specified system/internal schemas) in the database it is run against. Ensure you have a full backup before executing this. Running this on your production database is highly discouraged unless you know exactly what you are doing and why. Consider running it only on development or staging databases.
-- WARNING: This script is DANGEROUS and IRREVERSIBLE.
-- It drops all tables in the current database, except for system tables
-- and common Supabase internal schemas (auth, storage, graphql, supabase).
-- Ensure you have a complete backup before running this script.
-- Execute this ONLY if you are absolutely sure you want to delete all user tables.
DO $$
DECLARE
table_record record;
drop_statement text;
BEGIN
RAISE NOTICE 'Starting table drop process...';
-- Loop through all tables in schemas that are NOT system/internal schemas
FOR table_record IN
SELECT
quote_ident(table_schema) AS schema_name, -- Safely quote schema name
quote_ident(table_name) AS table_name -- Safely quote table name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE' -- Only target actual tables, not views etc.
AND table_schema NOT IN (
'pg_catalog', -- PostgreSQL system catalog
'information_schema', -- SQL standard information schema
'pg_toast', -- System toast tables
'auth', -- Supabase Auth schema
'storage', -- Supabase Storage schema
'graphql', -- Supabase GraphQL schema
'supabase' -- Supabase internal functions/migrations schema
-- Add any other schema names you wish to preserve here
-- e.g., 'my_important_schema'
)
ORDER BY
table_schema, table_name -- Process tables in a predictable order (optional)
LOOP
-- Construct the DROP TABLE statement with CASCADE and IF EXISTS
drop_statement := format('DROP TABLE IF EXISTS %s.%s CASCADE;',
table_record.schema_name,
table_record.table_name);
-- Log the statement being executed (optional but recommended)
RAISE NOTICE 'Executing: %', drop_statement;
-- Execute the dynamic SQL statement
EXECUTE drop_statement;
END LOOP;
RAISE NOTICE 'Finished dropping tables.';
END;
$$;
-- Optional: You might also want to drop other objects like sequences, views, functions etc.
-- Example for sequences (use with the same caution!):
/*
DO $$
DECLARE
seq_record record;
drop_statement text;
BEGIN
RAISE NOTICE 'Starting sequence drop process...';
FOR seq_record IN
SELECT
quote_ident(sequence_schema) AS schema_name,
quote_ident(sequence_name) AS sequence_name
FROM
information_schema.sequences
WHERE
sequence_schema NOT IN (
'pg_catalog',
'information_schema',
'pg_toast',
'auth',
'storage',
'graphql',
'supabase'
)
ORDER BY
sequence_schema, sequence_name
LOOP
drop_statement := format('DROP SEQUENCE IF EXISTS %s.%s CASCADE;',
seq_record.schema_name,
seq_record.sequence_name);
RAISE NOTICE 'Executing: %', drop_statement;
EXECUTE drop_statement;
END LOOP;
RAISE NOTICE 'Finished dropping sequences.';
END;
$$;
*/
-- Note: You would need separate scripts for views, functions, types, etc.,
-- following a similar pattern, carefully considering dependencies and system objects.
Explanation:
DO $$ ... $$;: This defines an anonymous code block using PL/pgSQL, allowing procedural logic like loops and dynamic execution.
DECLARE ...: Declares variables table_record (to hold information about each table) and drop_statement (to build the SQL command string).
FOR table_record IN SELECT ...: This loop iterates through the results of the SELECT query.
information_schema.tables: This is a standard SQL view providing metadata about tables and views in the database.
WHERE table_type = 'BASE TABLE': Filters the results to include only actual tables, excluding views, foreign tables, etc.
AND table_schema NOT IN (...): This is the crucial part for safety. It excludes tables belonging to:
pg_catalog and information_schema: Essential PostgreSQL system schemas.
pg_toast: Used internally by PostgreSQL for TOAST compression.
auth, storage, graphql, supabase: Common schemas used by Supabase for its core features. You can add other schemas here if you need to protect them.
quote_ident(...): This function ensures that schema and table names are correctly quoted (e.g., if they contain spaces, uppercase letters, or reserved keywords), preventing SQL injection or syntax errors.
format('DROP TABLE IF EXISTS %s.%s CASCADE;', ...): Constructs the DROP TABLE command.
IF EXISTS: Prevents errors if a table has already been dropped.
%s.%s: Placeholders for the schema and table names.
CASCADE: This tells PostgreSQL to automatically drop any objects that depend on the table being dropped (e.g., foreign key constraints referencing this table from other tables, views, etc.). This is key to "cascading delete".
RAISE NOTICE '...': Prints messages to the client, showing which table is about to be dropped. This is very helpful for monitoring.
EXECUTE drop_statement;: Executes the dynamically generated DROP TABLE command.
How to Use:
- Connect: Connect to your Supabase PostgreSQL database using a SQL client (like
psql, DBeaver, pgAdmin, or the Supabase SQL Editor).
- Backup: Create a backup first!
- Execute: Copy and paste the entire script (including the
DO $$...$$; block) into your SQL client and run it.
- Verify: Check if the intended tables have been dropped.
Remember to adjust the list of schemas in the NOT IN clause if there are other schemas you need to preserve.