BigQuery · DML statement
BigQuery TRUNCATE TABLE
TRUNCATE TABLE removes every row from a BigQuery table but keeps its schema, partitioning, clustering, description and labels. It is a metadata operation, so it costs nothing, unlike DELETE WHERE TRUE, which is billed as DML.
TRUNCATE TABLE [[project.]dataset.]table empties a table in one step. The table keeps everything except its rows: schema, column descriptions, partitioning and clustering spec, options, labels, IAM policies and row access policies. That makes it the usual first step of a full reload: truncate, then INSERT or load the new data, ideally inside one transaction so readers never see an empty table.
The docs describe TRUNCATE TABLE as a metadata operation that does not incur a charge, which is the main difference from DELETE FROM t WHERE TRUE: that DELETE is billed on demand for the size of the table. Compared with CREATE OR REPLACE TABLE, truncation keeps table-level settings you would otherwise have to restate. Truncating does not reset quotas tied to the table name.
It works on standard tables only: views, materialized views, models and external tables cannot be truncated. It counts as a mutating DML statement, so it waits for or conflicts with UPDATE, DELETE and MERGE running on the same table, and rows streamed within the last 30 minutes through the legacy tabledata.insertAll API block it. Truncated data remains recoverable with time travel for the dataset's time travel window, and materialized views on the table are invalidated until their next refresh.
Syntax
TRUNCATE TABLE [[project_name.]dataset_name.]table_nameArguments and return value
| Argument | Description |
|---|---|
table_name | A standard table. Views, materialized views, models and external tables are not supported. |
Returns: No result set. The table exists with zero rows.
Examples
Empty a staging table
TRUNCATE TABLE `project.staging.orders_import`;Full reload without an empty window
Readers keep seeing the old data until COMMIT, and a failure rolls back to it.
BEGIN TRANSACTION;
TRUNCATE TABLE `project.dataset.exchange_rates`;
INSERT INTO `project.dataset.exchange_rates` (currency, rate_to_eur, as_of)
SELECT currency, rate_to_eur, as_of
FROM `project.staging.exchange_rates_latest`;
COMMIT TRANSACTION;Recover truncated rows with time travel
Within the time travel window, read the table as it was before the truncation into a new table, then insert from that copy. A statement can't read one table at two points in time, so INSERT INTO t SELECT * FROM t FOR SYSTEM_TIME AS OF ... fails.
CREATE TABLE `project.dataset.exchange_rates_restored` AS
SELECT *
FROM `project.dataset.exchange_rates`
FOR SYSTEM_TIME AS OF TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR);Common mistakes
- Using
DELETE FROM t WHERE TRUEto empty a table. It works but is billed as DML for the size of the table; TRUNCATE TABLE is free. - Truncating to drop columns or change partitioning. The schema and layout survive; use CREATE OR REPLACE TABLE for structural changes.
- Truncating a view or materialized view. It isn't supported; truncate the base table or recreate the view.
- Truncating a table that still receives legacy streaming inserts. Rows streamed in the last 30 minutes block DML, including TRUNCATE.
Cost and performance
- TRUNCATE TABLE does not incur a charge, while
DELETE ... WHERE TRUEon a 1 TiB table bills at least 1 TiB on demand ($6.25 at $6.25 per TiB). - On physical storage billing, truncated bytes stay billable during the time travel window and fail-safe period. Shorten the dataset's time travel window if large staging tables are truncated daily.
FAQ
What is the difference between TRUNCATE and DELETE in BigQuery?
TRUNCATE TABLE removes all rows as a free metadata operation. DELETE removes the rows matching a WHERE clause and is billed as DML.
Does TRUNCATE TABLE keep the schema and partitioning?
Yes. Schema, partitioning, clustering, description, labels and policies are unchanged; only the rows are removed.
Related
Errors you may hit
More DML statements
All BigQuery DML statements · All statements and clauses · BigQuery error messages
