DEV Community

abdullah khrais
abdullah khrais

Posted on

3 1 1 1 1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay