Delete or Truncate All Data in a Database

A quick tip for you all, ever need to delete or truncate all the data from a database? There has been a few times during testing after my database grew, that I needed a quick solution to clear a database. The solution is using the MSForEachTable stored procedure. Simply do the following :

DatabaseName – The name of the database you need to clear out.

To Delete :

USE DatabaseName
EXEC sp_MSForEachTable 'DELETE FROM ?'

To Truncate :

USE DatabaseName
EXEC sp_MSForEachTable 'TRUNCATE TABLE ?'

Happy Coding!

Releated Post

Leave a Reply

%d bloggers like this: