Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:36:15+00:00 2026-05-31T09:36:15+00:00

How do I drop all tables whose name start with, say, doors_ ? Can

  • 0

How do I drop all tables whose name start with, say, doors_? Can I do some sort of regex using the drop table command?

I prefer not writing a custom script but all solutions are welcomed. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:36:16+00:00Added an answer on May 31, 2026 at 9:36 am

    This script will generate the DDL commands to drop them all:

    SELECT 'DROP TABLE ' || t.oid::regclass || ';'
    FROM   pg_class t
    -- JOIN   pg_namespace n ON n.oid = t.relnamespace -- to select by schema
    WHERE  t.relkind = 'r'
    AND    t.relname ~~ E'doors\_%' -- enter search term for table here
    -- AND n.nspname ~~ '%myschema%' -- optionally select by schema(s), too
    ORDER  BY 1;
    

    The cast t.oid::regclass makes the syntax work for mixed case identifiers, reserved words or special characters in table names, too. It also prevents SQL injection and prepends the schema name where necessary. More about object identifier types in the manual.

    About the schema search path.

    You could automate the dropping, too, but it’s unwise not to check what you actually delete before you do.

    You could append CASCADE to every statement to DROP depending objects (views and referencing foreign keys). But, again, that’s unwise unless you know very well what you are doing. Foreign key constraints are no big loss, but this will also drop all dependent views entirely. Without CASCADE you get error messages informing you which objects prevent you from dropping the table. And you can then deal with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I drop all tables from a database using manage.py and command line?
How can I drop all tables whose names begin with a given string? I
How can I drop all user tables in oracle? I have problem with constraints.
How can I drop all tables in my database ? 1) In one MYSQL
Is there an easy way to reset a django database (i.e. drop all data/tables,
Why does SQL 2008 all of a sudden want to drop my tables when
I am trying to drop all tables in schema with no rows,but when i
How do I drop selected tables in Rails? I want to drop all tables
Is there a nice easy way to drop all tables from a MySQL database,
I want to keep t1,t2 and drop all other tables.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.