| Introduction Hi to all, here I am with a new article; topic of this article is “Difference between Truncate and Delete”. Truncate an Delete both are used to delete data from the table. These both the command will only delete the data of the specified table, they cannot remove the whole table data along with its structure. Now it’s ok that both the SQL statements are used to delete only the data from the table but they both differ from each other in many aspects like syntax, performance, resources uses etc. So, first let’s take a look of both of these terms (Truncate and Delete), |
Truncate Truncate command in SQL removes all rows from a table without logging the individual row deletion in the transaction log. Truncate statement having the sane functionality as the Delete command has that is it deletes the data from the table without modifying or deleting the structure of the table. You can’t use the Where Clause with this (Truncate) statement. Syntax: TRUNCATE TABLE [ { database_name.[ schema_name ]. | schema_name . } ] table_name Table_name : Is the name of the table to truncate or from which all rows are removed. Simple it looks like below querry. The above command will delete all data from the table author. |
Delete Delete command in SQL also removes all rows from a table with logging the individual row deletion in the transaction log. You can use the Where Clause with this (Delete) statement. In case of delete I am writing just the simple syntax for more detailed explanation of delete syntax visit this link: DELETE (Transact-SQL). Syntax: DELETE FROM TABLE_NAME[ { database_name.[ schema_name ]. | schema_name . } ] table_name Table_name : Is the name of the table to truncate or from which all rows are removed. Simple it looks like below querry. The above command will delete all data from the table author. In case of delete statements you can limit your delete query using where clause to delete, only particular records that fulfills the condition of where clause will be deleted not the all records. It looks like below querry with where clause. |
Differences between Truncate and Delete Now above you have seen the delete and truncate statements. Both the statements are functionally same, but still there are many differences exists between both of these, those differences with explanation are as follows: |
Delete and Truncate removes the data not the structure Both commands removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement. |
Conditional based deletion of data So, the conditional based deletion means, let’s suppose I am having a table authors and from this table I want to delete the authors that are living in Australia. So can we perform conditional based deletion with truncate and delete?? Answer is as follows: |
|
Delete and Truncate both are logged operation Normally on most of the articles I read on internet I have seen that they says “delete is a logged operation and truncate is not a logged operation”, that means when we run the delete command it logs (records) the information about the deleted row and when we run the truncate command it doesn’t logs (records) any data. But this is not true, truncate is also a logged operation but in a different way and it uses fewer system and transaction log resources than delete. Because the truncate use minimum logging resources that is why it is faster than delete. So both delete and truncate are logged operation, how logging of both of these commands happen, let’s take a look: |
To be more specific lets take a look of an example, which will tell you that truncate is also a logged operation no matters that it is not fully logged like Delete but still it’s a logged operation. So take a look of following example. /*Create a dummy table in a non-production(dummy) database.*/ /*Now insert the records in the tranTest table.*/ /*Then as you know delete is a looged operation that means /*Now delte all records from teh table.*/ /*And Insert new fresh records in teh table.*/ /*Then as you now you know that Truncate is also a logged /*So when we run the above command then Truncate also restores So now I think it’s enough discussion on this topic to prove that truncate is also a logged operation in SQL Server. Let’s move to other difference. Behavior of Delete and Truncate for identity columns Example for this is here: /*First if u already created the tranTest table then delte it /*Then Create a dummy table in a non-production(dummy) database with an Identity column*/ /*Now insert the records in the tranTest table.*/ /*If you run the below querry you’ll see that the /*Now delete all the records from the table using the delete command.*/ /*Now by running the above command your table is empty, /*After running the below query u’ll see that your identity now /*Now drop your table again and create it again.*/ /*Create a dummy table again in a non-production(dummy) database /*And Insert new fresh records in teh table.*/ /*Now at this point the counter of this table’s /*Then truncate the table.*/ /*and insert new records*/ /*Now u’llsee that after truncating the table the /*So this example explains the beahviour of both of |
TRUNCATE is a DDL command whereas DELETE is a DML command This is also a common difference you have read in many articles. That is TRUNCATE is a DDL (data definition language) operation and DELETE is a DML (data manipulation language) operation. Yes according to SQL server it’s true. But why it is so, why TRUNCATE is DDL and DELETE is DML? Let’s look at this; Actually when we run the TRUNCATE command it puts a “Schema modification (Sch-M)” lock on the table. Now, what is “schema modification (Sch-M)”? The Database Engine uses schema modification (Sch-M) locks during a table data definition language (DDL) operation, such as adding a column or dropping a table. During the time that it is held, the Sch-M lock prevents concurrent access to the table. This means the Sch-M lock blocks all outside operations until the lock is released. Now you’ll ask that it blocks any modification to the table whereas in case of TRUNCATE we are performing modifications because we are deleting the data? So, to become a DDL operation you have to fulfill some of the conditions written below: |
Here the TRUNCATE is doing all the activities that proofs that TRUNCATE is a DDL operation. |
| Now we move to the DELETE command. In case of delete command I am not sure which lock is implemented, but as we know and you can read above that DELETE command deletes the row one by one, that is its modifying the data by deleting it from the table, because the DELETE perform data modification that is why the DELETE TRIGGER is called when we issue a DELETE command for a table. DELETE command does not modify the table structure in any manner, as like TRUNCATE modifies the identity column by resetting its value but the DELETE doesn’t do this. So, to become a DML operation you have to fulfill some of the conditions written below: |
Here the DELET modifying the data of the table and also when delete statement is running you can’t modify the table structure. So we can say that DELETE is a DML operation. |
Behavior of Truncate and Delete for Triggers Now Triggers, another most important topic in SQL Server, but right now I am not talking about triggers, I am talking about How both TRUNCATE and DELETE behaves differently for Triggers and Why is it so??? As you all know that triggers fire whenever any data modification happens in the table. In case of TRUNCATE and DELETE because they are deleting the data from the table so, the DELETE TRIGGER will fire if present and also the INSTEAD OF and AFTER triggers will be triggered if present for DELETE TRIGGER. INSERT and UPDATE will not be fired here.So let’s take both one by one: |
So the conclusion is in case of Truncate triggers will not be fired and in case of Delete they will be fired if present. |
Where we can use these statements Where we can and where we can’t use these statements. There are some restrictions on the use of both of these statements; restrictions for both statements are as follows: For DELETE: |
|
| For TRUNCATE You cannot use TRUNCATE TABLE on tables that: |
So, keep in mind these restrictions and then use the Delete or Truncate statements. |
Permissions of performing TRUNCATE or DELETE operation For using both the statements you need some permission from the server, which decides that you can perform the Delete or Truncate action on the table or not. To truncate a table, you need at least ALTER permissions on the table (which is granted by default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles). To delete rows using the delete statement, you only need the DELETE permission.The above is the detailed explanation of the differences between both of these statements. But if you need a basic knowledge about the differences between both of these then those are as follows, |
Differences between Truncate and Delete in brief |
Now this article is completed. Please give your comments about this article. |
|
******* |
SQL – Truncate and Delete
October 10, 2007 by Prashant Pandey

Hi,
Thanks for the nice article. Very informative.
I have a question. Will use of TRUNCATE TABLE and then DROP TABLE be faster and use less resources than just the DROP TABLE statement?
I got this doubt because Truncate will not log delete of each individual row.
Thanks
Shashank
Hi shashank..
I think both DROP and TRUNCATE not logs the data, caz both are the DDL commands. But the difference is that TRUNCATE only deletes the rows from the table whereas DROP deletes the rows as well as structure of the table, In both cases triggers are not fired which proves that they are DDL command. so you can say that using TRUNCATE then DROP will be not a effective idea, directly use the DROP. wht i think..
this is very usefull Artical… lot of thaks
Its a really nice article, i really appreciate all the efforts.
Very usefull. However, I still have a dude. If I want to delete old records (Example: … where dbf.date < xx/xx/xx ) forever, to make space in my hard disk, how can I do that?. I understand DELETE let the records some where there.
Hi,
Thanks for such a detailed article about differences b/w DELETE and TRUNCATE. I had searched this many times on net but the explaination given here is far more better, useful and best part is written in such a way that even a layman can understand with just one reading.
Good Work Prashant……..
Any idea why a truncate on a table would throw a foreign key constraint error but a delete of all rows on the same table works fine? I verified that all FK tables had no rows in them when I truncated. Is it possible you cannot truncate a table that has FK’s?
Its very useful article….
Many many Thanks….
Can we Rollback the data after using Truncate statement…
Reply pls….
Hi Prashant,
In case I am not worried about FK constraints / triggers, then is there any difference between TRUNCATE and DELETE in terms of performance of following step (INSERT). For this step, I understand TRUNCATE will be faster but If I want to reload (INSERT) almost the same number of records, will my INSERT be faster and compensate for time I lost in DELETE (compared to TRUNCATE)? Any scenario where DELETE ALL will be preferred over TRUNCATE, other than logging or ones you covered above?
Thanks for your article anyways.
Hi RAMESH M
Yes you can. As explained in above article.
Very useful indeeeeeedddddddd………….
[...] back the transaction? > > truncate is *minimaly* logged, that doesn’t mean it isn’t
http://techahead.wordpress.com/2007/…te-and-delete/ « SQL Server Management Studio | Select Distinct with a twist [...]
Hi Prashant
Nice explanation. But hope you can answer couple of questions I have on this.
Truncate does not delete data. Truncate just deallocates pages and stores this information in log file.
1) If data is not deleted then does that mean that data still
exist somewhere?
2) In truncate, if data is not deleted and only pages gets
deallocated(information of which is stored in log file),
then why is it that we cannot use rollback to reallocate
these pages?
Fentastic! I found the absolute differences bet’ the TRUNCATE and DELETE commands. No one author provided such a correct answer for this question. Its very useful for the beginners. Thank you very much!