I am trying to truncate an existing table in GBQ but the below command fails when I run it. Is there any specific command or syntax to do that. I looked into GBQ documentation but no luck.
TRUNCATE TABLE [dw_test.test];
To remove all data from an existing table, use the SQL TRUNCATE TABLE order. You can also use the DROP TABLE command to delete an entire table. But Truncate will remove the entire table structure from the database, and you will need to recreate the table if you want to store any data.
You can delete a BigQuery table in the following ways: BigQuery Delete Table: Using the Cloud Console. BigQuery Delete Table: Using a Data Definition Language (DDL) DROP TABLE statement. BigQuery Delete Table: Using the bq command-line tool bq rm command.
TRUNCATE TABLE 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.
While BigQuery didn't used to support anything other than SELECT
s, it now does as long as you uncheck "Use Legacy SQL" in the query options. There is no truncation, but you can delete:
DELETE from my_table WHERE 1=1
Note that BigQuery requires the use of WHERE
in the DELETE
, so if you want to delete everything you need to use a statement that will always be true.
Good news, TRUNCATE TABLE is supported by now: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#truncate_table_statement
TRUNCATE TABLE [[project_name.]dataset_name.]table_name
However, please note that this will not work / is not supported, if a partition filter is required through your table definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With