Skip to content

Instantly share code, notes, and snippets.

@kurtroberts
Created August 4, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtroberts/7eb48b27af95a43df7daad5dfefdc3fb to your computer and use it in GitHub Desktop.
Save kurtroberts/7eb48b27af95a43df7daad5dfefdc3fb to your computer and use it in GitHub Desktop.
Drop all tables in a PSQL schema
-- Liberally borrowed from - https://stackoverflow.com/questions/3327312/drop-all-tables-in-postgresql
-- If you happen to be working on a jira database, you're going to have to do it twice to resolve all the cascades
DO $$ DECLARE
r RECORD;
BEGIN
-- if the schema you operate on is not "current", you will want to
-- replace current_schema() in query with 'schematodeletetablesfrom'
-- *and* update the generate 'DROP...' accordingly.
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment