Difference Between DELETE and TRUNCATE

Difference Between DELETE and TRUNCATE

6 mins read191 Views Comment
Anshuman
Anshuman Singh
Senior Executive - Content
Updated on Feb 3, 2025 09:40 IST

The main difference between Delete and Truncate is that the DELETE command in SQL deletes specific rows from a table based on a specific condition using the WHERE clause. On the other hand, the TRUNCATE command deletes all the rows from the table.

2023_04_MicrosoftTeams-image-6.jpg

When working with relational databases, it’s essential to know the difference between Delete and Truncate. Both commands can be used to remove data from a table, but they work differently and have different use cases. This article will explore the differences between delete and truncate and when to use each command.

Table of contents

Recommended online courses

Best-suited Database and SQL courses for you

Learn Database and SQL with these high-rated online courses

– / –
2 months
4.24 K
6 weeks
– / –
15 hours
– / –
– / –
– / –
1 hours
– / –
30 hours
– / –
31 hours
– / –
80 hours

Difference Between DELETE and TRUNCATE

For better understanding, let's explore the differences between the DELETE and TRUNCATE commands in a tabular format:

Feature DELETE TRUNCATE
Type DML (Data Manipulation Language) DDL (Data Definition Language)
Operation Removes specific rows based on a condition (if provided) Removes all rows from the table
WHERE Clause Supports WHERE clause to delete selected records Does not support WHERE clause; deletes all rows
Rollback (UNDO) Can be rolled back using ROLLBACK (if inside a transaction) Cannot be rolled back once executed (Auto-commit)
Logging Logs each deleted row in the transaction log Minimal logging, does not log individual row deletions
Performance Slower for large datasets due to logging and row-by-row deletion Faster as it deallocates entire data pages
Identity Column Reset Does not reset IDENTITY column values Resets IDENTITY column values to the seed value
Triggers Triggers are fired on DELETE operations Triggers do not fire on TRUNCATE operations
Foreign Key Constraints Can be used when there are foreign key constraints (with ON DELETE CASCADE) Cannot be used if the table has foreign key constraints
Table Structure Retains table structure and associated constraints Retains table structure but resets auto-increment values
What are Different Types of Databases?
MongoDB Vs. MySQL: Applications and Differences
MariaDB vs MySQL – What’s the Difference?

If you are interested in learning how to effectively access, manipulate, and retrieve data from databases, pursue Mysql Online Courses & Certifications.

What is the DELETE Command in SQL?

The DELETE command removes one or more rows from a table. 

DELETE is a DML (Data Manipulation Language) command that deletes rows/tuples from a specified table or relation in SQL. You can delete any number of rows using the DELETE command. It is mostly combined with a WHERE clause to delete specific rows that match a condition. If you don’t add a WHERE clause to your query, the DELETE command deletes all rows and empties the table.

You must have delete permission on the table to use this command. When using the DELETE command, you must specify which rows you want to delete using a WHERE clause. If you omit the WHERE clause, the command will delete all rows in the table, which is rarely what you want to do. Syntax: DELETE FROM table_name WHERE condition;

DELETE Command Example in SQL 

Observe the employees table:

ID Name Department Salary
1 Rahmi HR 50000
2 Atul IT 60000
3 Aquib Finance 55000
4 Jaya IT 62000

Command: DELETE FROM employees WHERE Department = 'IT';

Output:

ID Name Department Salary
1 Rashmi HR 50000
3 Aquib Finance 55000

Note:

  • Only IT department employees (Atul & Jaya) are deleted.
  • Auto-increment value remains unchanged.
  • Can be rolled back if inside a transaction

If you are preparing for job roles where MySQL expertise is essential, like database administrator, data analyst, or SQL developer, you must check out the  Top MySQL Interview Questions and Answers article. 

Use Cases of DELETE

  1. Removing unwanted or duplicate data:  Sometimes the table is no longer needed, or duplicate lows need removal. That can be removed using a delete statement.
  2. Implementing data retention policies: you might need to delete old employee data after a certain period has elapsed.
  3. Cleaning up after testing or development: You may create temporary data in a table that needs to be removed when you’re done during testing or development. The DELETE statement can be used to remove this data.
  4. Maintaining data integrity: If you have foreign key constraints between tables, you may need to delete rows from one table when corresponding rows are deleted from another. The DELETE statement can be used to implement this behaviour.
  5. Undoing mistakes: If you make a mistake while inserting or updating data, you can use the DELETE statement to remove the incorrect data and start over.

What is the TRUNCATE command in SQL?

The TRUNCATE command is used to remove all rows from a table.

SQL Truncate is a DDL (Data Definition Language) command. Delete all rows in the table. SQL Server stores table data in pages. The truncate command deallocates pages and deletes rows. Make an entry in the transaction log to free the page.

Not all row deletions are recorded in the transaction log. Unlike the DELETE command, the TRUNCATE command does not use a WHERE clause, removing all data from the table in one operation. When you use the TRUNCATE command, the table structure is preserved, but all data is lost. Syntax: TRUNCATE TABLE table_name;

TRUNCATE Command Example in SQL

Observe the employees table:

ID Name Department Salary
1 Rahmi HR 50000
2 Atul IT 60000
3 Aquib Finance 55000
4 Jaya IT 62000

Command: TRUNCATE TABLE employees;

Output:

ID Name Department Salary

Note: 

  • All records are deleted.
  • Auto-increment counter is reset to 1.
  • Cannot be rolled back

Use Case of TRUNCATE

  1. Resetting Data: In the Case of table resetting, you need to remove the data and then use the truncate command. For example, you might want to clear the contents of a log table periodically to free up space.
  2. Removing Large Data Sets: If a large data set is no longer needed, you can quickly use the truncate command to remove it from your database quickly and efficiently. 
  3. Removing Test Data: While testing the application in a test environment, you can use a truncate command to remove all the test data. This ensures that you start with a clean slate each time you run a test.
  4. Resetting auto-increment counters: While using an auto-increment column in a table to generate primary keys, truncating the table resets the counter for that column to the initial value.

Conclusion

Now that you know the main difference between the truncate and delete commands in SQL queries, remember that TRUNCATE is a DDL command. On the other hand, the DELETE command is a DML command. This distinction between delete vs truncate helps us assess these commands’ usage and broader meaning. Therefore, although you can use the DELETE command to remove one or more rows from a table, you may need to reset the table using the TRUNCATE command in certain circumstances.

Keep Learning!!

Keep Sharing!!

Related Reads

What is the Difference Between SQL and MySQL?
Difference between SQL and NoSQL
Difference Between DELETE and TRUNCATE

FAQs on Difference Between DELETE and TRUNCATE

What is the main difference between DELETE and TRUNCATE?

DELETE removes specific rows based on a condition. TRUNCATE removes all rows in a table without logging individual row deletions. This makes TRUNCATE faster and more efficient for clearing tables.

Can you roll back a TRUNCATE operation?

No, TRUNCATE cannot be rolled back if executed outside a transaction. It is a non-logged operation. In contrast, DELETE can be rolled back if it is part of a transaction.

Does TRUNCATE reset identity columns?

Yes, TRUNCATE resets any identity columns to their seed value. This means that the next inserted row will start with the initial value. DELETE does not reset identity columns.

When should I use DELETE over TRUNCATE?

Use DELETE when you need to remove specific rows or when you want to maintain a transaction log. It is also suitable when you need to trigger DELETE triggers.

Are there performance differences between DELETE and TRUNCATE?

Yes, there are. TRUNCATE is generally faster than DELETE because it does not log individual row deletions. It simply deallocates the data pages. DELETE can be slower, especially with large datasets.

About the Author
author-image
Anshuman Singh
Senior Executive - Content

Anshuman Singh is an accomplished content writer with over three years of experience specializing in cybersecurity, cloud computing, networking, and software testing. Known for his clear, concise, and informative wr... Read Full Bio