TRUNCATE TABLE

SQL · Working with Tables

Theory

TRUNCATE TABLE removes every row from a table in one go while keeping the table itself: columns, data types, indexes, constraints and permissions all stay. TRUNCATE TABLE sales_temp leaves an empty sales_temp ready for the next load.

It is much faster than DELETE FROM sales_temp on large tables because it deallocates whole data pages instead of removing and logging each row, and it does not fire row-level DELETE triggers. It takes no WHERE clause; it is all or nothing. It is usually blocked when other tables reference this one with foreign keys. In SQL Server it resets identity columns to their seed; PostgreSQL resets them only with RESTART IDENTITY. Whether it can be rolled back depends on the engine: PostgreSQL and SQL Server allow it inside a transaction, while Oracle and MySQL commit it immediately. SQLite has no TRUNCATE; DELETE without WHERE is optimised instead.

Why we use it

Staging and temporary tables are reloaded constantly. TRUNCATE empties them almost instantly, without the time, log space and trigger side effects of deleting millions of rows one by one.

When to use it

Use TRUNCATE to reset staging tables, clear test data or wipe logs completely. Use DELETE when you need a WHERE clause, triggers to fire or guaranteed rollback, and DROP TABLE when the table itself should go.

How it works

  1. TRUNCATE TABLE sales_temp
  2. Referenced by foreign keys? Yes: Blocked (PostgreSQL offers CASCADE). No: Continue.
  3. Deallocate all data pages at once
  4. Keep columns, indexes, constraints and permissions
  5. SQL Server, or RESTART IDENTITY? Yes: Reset the identity counter. No: Counter keeps its value.
  6. Empty table, ready for the next load

Syntax

TRUNCATE TABLE table_name;

Example

TRUNCATE TABLE sales_temp;
-- Clears every row from sales_temp instantly,
-- ready for tomorrow's data load — the schema is untouched.

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic