drop all the tables in a database

Posted on 2016-07-15
Medium Priority
Last Modified: 2016-07-18
I need a script to drop all the tables in a database. What would be the easiest way to do that?
Question by:vbnetcoder
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 70

Expert Comment

by:Éric Moreau
ID: 41713142
wouldn't it be easier to drop the database and recreate it?
LVL 70

Expert Comment

by:Éric Moreau
ID: 41713146
or you can try:
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"

Open in new window


Assisted Solution

vbnetcoder earned 0 total points
ID: 41713148
Eric you might be right on dropping the database
LVL 10

Accepted Solution

Jason clark earned 2000 total points
ID: 41713829
Try below script to drop all tables:

SELECT ' Drop table ' + s.NAME + '.' + t.NAME
FROM   sys.tables t
       JOIN sys.schemas s
         ON t.[schema_id] = s.[schema_id]
WHERE  t.type = 'U' 

Exec sp_executesql @sql

Open in new window

But remember If you have any foreign Keys defined between tables then first run the below query to disable all foreign keys present in your DB.


Open in new window


Expert Comment

by:Jose Torres
ID: 41717390
Remember by dropping the database you loose all objects (views, stored procedures.....)

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question