DEV Community

abdullah khrais
abdullah khrais

Posted on

Hey everyone, Check out this SQL script to delete all tables and indexes in a schema. Great for fast cleanup in dev/testing!

BEGIN
    FOR t IN (SELECT table_name FROM user_tables) LOOP
        EXECUTE IMMEDIATE 'DROP TABLE ' || t.table_name || ' CASCADE CONSTRAINTS';
    END LOOP;

    FOR i IN (SELECT index_name FROM user_indexes) LOOP
        EXECUTE IMMEDIATE 'DROP INDEX ' || i.index_name;
    END LOOP;
END;
Enter fullscreen mode Exit fullscreen mode

*Explanation:
*

  1. Tables: The script iterates through all tables in the current schema (user_tables) and drops each table using DROP TABLE ... CASCADE CONSTRAINTS to handle dependencies.

  2. Indexes: Similarly, it iterates through all indexes (user_indexes) and drops each one using DROP INDEX.

Top comments (0)