Delete all rows from all tables in a SQL Server Database

Delete all rows from all tables SQL server

Have you ever come across the problem to delete all rows from all tables available in a database? One of my colleagues asked me this question that how can we delete all records from multiple tables of a database. So, in this SQL tutorial, we are going to discuss, how we can delete all rows from all tables of a SQL Server database.

Follow below steps with t-SQL delete all records from all tables from SQL server:

Disable all constraints first:-

1

EXEC sys.sp_msforeachtable'ALTER TABLE ? NOCHECK CONSTRAINT ALL'

Execute a Delete or truncate operation on each table of the database:-

1

EXEC sys.sp_msforeachtable'DELETE FROM ?'

Enable all constraints on the database:-

1

EXEC sys.sp_MSForEachTable'ALTER TABLE ? CHECK CONSTRAINT ALL'

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.