Restore deleted tables
This document describes how to restore (or undelete) a deleted table in BigQuery. You can restore a deleted table within the time travel window specified for the dataset, including explicit deletions and implicit deletions due to table expiration. You can also configure the time travel window.
For information about how to restore an entire deleted dataset or snapshot, see the following resources:
The time travel window can have a duration between two and seven days. After the time travel window has passed, BigQuery provides a fail-safe period where the deleted data is automatically retained for an additional seven days. Once the fail-safe period has passed, it isn't possible to restore a table using any method, including opening a support ticket.
Before you begin
Ensure that you have the necessary Identity and Access Management (IAM) permissions to restore a deleted table.
Required roles
To get the permissions that
you need to restore a deleted table,
ask your administrator to grant you the
BigQuery User (roles/bigquery.user
)
IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Restore a table
When you restore a table from historical data, tags from the source table aren't copied to the destination table.
You can restore a table that was deleted but is still within the time travel
window by copying the table to a new table, using the @<time>
time decorator.
To copy the table, you can use the bq command-line tool or the client libraries:
Console
You can't undelete a table by using the Google Cloud console.
bq
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
To restore a table, first determine a UNIX timestamp of when the table existed (in milliseconds). You can use the Linux
date
command to generate the Unix timestamp from a regular timestamp value:date -d '2023-08-04 16:00:34.456789Z' +%s000
Then, use the
bq copy
command with the@<time>
time travel decorator to perform the table copy operation.For example, enter the following command to copy the
mydataset.mytable
table at the time1418864998000
into a new tablemydataset.newtable
.bq cp mydataset.mytable@1418864998000 mydataset.newtable
(Optional) Supply the
--location
flag and set the value to your location.You can also specify a relative offset. The following example copies the version of a table from one hour ago:
bq cp mydataset.mytable@-3600000 mydataset.newtable
For more information, see Restore a table from a point in time.
Go
Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Go API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Java
Before trying this sample, follow the Java setup instructions in the
BigQuery quickstart using
client libraries.
For more information, see the
BigQuery Java API
reference documentation.
To authenticate to BigQuery, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
Node.js
Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Python
Before trying this sample, follow the Python setup instructions in the
BigQuery quickstart using
client libraries.
For more information, see the
BigQuery Python API
reference documentation.
To authenticate to BigQuery, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
If you anticipate that you might want to restore a table later than what is allowed by the time travel window, then create a table snapshot of the table. For more information, see Introduction to table snapshots.
What's next
- Learn how to create and use tables.
- Learn how to manage tables.
- Learn how to modify table schemas.
- Learn about working with table data.