Delete all data in SQL Server database

AndreyAkinshin picture AndreyAkinshin · Sep 10, 2010 · Viewed 207.8k times · Source

How I can delete all records from all tables of my database? Can I do it with one SQL command or I need for one SQL command per one table?

Answer

Ryan Kirkman picture Ryan Kirkman · Aug 2, 2012

SQLMenace's solution worked for me with a slight tweak to how data is deleted - DELETE FROM instead of TRUNCATE.

-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' 
GO 

EXEC sp_MSForEachTable 'DELETE FROM ?' 
GO 

-- enable referential integrity again 
EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL' 
GO