Undelete

Undelete a table.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

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 .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "time" 
  
 "cloud.google.com/go/bigquery" 
 ) 
 // deleteAndUndeleteTable demonstrates how to recover a deleted table by copying it from a point in time 
 // that predates the deletion event. 
 func 
  
 deleteAndUndeleteTable 
 ( 
 projectID 
 , 
  
 datasetID 
 , 
  
 tableID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // datasetID := "mydataset" 
  
 // tableID := "mytable" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 bigquery 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "bigquery.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 ds 
  
 := 
  
 client 
 . 
 Dataset 
 ( 
 datasetID 
 ) 
  
 if 
  
 _ 
 , 
  
 err 
  
 := 
  
 ds 
 . 
 Table 
 ( 
 tableID 
 ). 
 Metadata 
 ( 
 ctx 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 err 
  
 } 
  
 // Record the current time.  We'll use this as the snapshot time 
  
 // for recovering the table. 
  
 snapTime 
  
 := 
  
 time 
 . 
 Now 
 () 
  
 // "Accidentally" delete the table. 
  
 if 
  
 err 
  
 := 
  
 client 
 . 
 Dataset 
 ( 
 datasetID 
 ). 
 Table 
 ( 
 tableID 
 ). 
 Delete 
 ( 
 ctx 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 err 
  
 } 
  
 // Construct the restore-from tableID using a snapshot decorator. 
  
 snapshotTableID 
  
 := 
  
 fmt 
 . 
 Sprintf 
 ( 
 "%s@%d" 
 , 
  
 tableID 
 , 
  
 snapTime 
 . 
 UnixNano 
 () 
 / 
 1e6 
 ) 
  
 // Choose a new table ID for the recovered table data. 
  
 recoverTableID 
  
 := 
  
 fmt 
 . 
 Sprintf 
 ( 
 "%s_recovered" 
 , 
  
 tableID 
 ) 
  
 // Construct and run a copy job. 
  
 copier 
  
 := 
  
 ds 
 . 
 Table 
 ( 
 recoverTableID 
 ). 
  CopierFrom 
 
 ( 
 ds 
 . 
 Table 
 ( 
 snapshotTableID 
 )) 
  
 copier 
 . 
 WriteDisposition 
  
 = 
  
 bigquery 
 . 
  WriteTruncate 
 
  
 job 
 , 
  
 err 
  
 := 
  
 copier 
 . 
 Run 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 err 
  
 } 
  
 status 
 , 
  
 err 
  
 := 
  
 job 
 . 
 Wait 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 err 
  
 } 
  
 if 
  
 err 
  
 := 
  
 status 
 . 
  Err 
 
 (); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 err 
  
 } 
  
 ds 
 . 
 Table 
 ( 
 recoverTableID 
 ). 
 Delete 
 ( 
 ctx 
 ) 
  
 return 
  
 nil 
 } 
 

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 .

  import 
  
 com.google.cloud.bigquery. BigQuery 
 
 ; 
 import 
  
 com.google.cloud.bigquery. BigQueryException 
 
 ; 
 import 
  
 com.google.cloud.bigquery. BigQueryOptions 
 
 ; 
 import 
  
 com.google.cloud.bigquery. CopyJobConfiguration 
 
 ; 
 import 
  
 com.google.cloud.bigquery. Job 
 
 ; 
 import 
  
 com.google.cloud.bigquery. JobInfo 
 
 ; 
 import 
  
 com.google.cloud.bigquery. Table 
 
 ; 
 import 
  
 com.google.cloud.bigquery. TableId 
 
 ; 
 import 
  
 org.threeten.bp.Instant 
 ; 
 // Sample to undeleting a table 
 public 
  
 class 
 UndeleteTable 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 datasetName 
  
 = 
  
 "MY_DATASET_NAME" 
 ; 
  
 String 
  
 tableName 
  
 = 
  
 "MY_TABLE_TABLE" 
 ; 
  
 String 
  
 recoverTableName 
  
 = 
  
 "MY_RECOVER_TABLE_TABLE" 
 ; 
  
 undeleteTable 
 ( 
 datasetName 
 , 
  
 tableName 
 , 
  
 recoverTableName 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 undeleteTable 
 ( 
 String 
  
 datasetName 
 , 
  
 String 
  
 tableName 
 , 
  
 String 
  
 recoverTableName 
 ) 
  
 { 
  
 try 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
  BigQuery 
 
  
 bigquery 
  
 = 
  
  BigQueryOptions 
 
 . 
 getDefaultInstance 
 (). 
 getService 
 (); 
  
 // Record the current time.  We'll use this as the snapshot time 
  
 // for recovering the table. 
  
 long 
  
 snapshotEpoch 
  
 = 
  
 Instant 
 . 
 now 
 (). 
 toEpochMilli 
 (); 
  
 // ... 
  
 // "Accidentally" delete the table. 
  
 bigquery 
 . 
  delete 
 
 ( 
 TableId 
 . 
 of 
 ( 
 datasetName 
 , 
  
 tableName 
 )); 
  
 // Construct the restore-from tableID using a snapshot decorator. 
  
 String 
  
 snapshotTableId 
  
 = 
  
 String 
 . 
 format 
 ( 
 "%s@%d" 
 , 
  
 tableName 
 , 
  
 snapshotEpoch 
 ); 
  
 // Construct and run a copy job. 
  
  CopyJobConfiguration 
 
  
 configuration 
  
 = 
  
  CopyJobConfiguration 
 
 . 
 newBuilder 
 ( 
  
 // Choose a new table ID for the recovered table data. 
  
  TableId 
 
 . 
 of 
 ( 
 datasetName 
 , 
  
 recoverTableName 
 ), 
  
  TableId 
 
 . 
 of 
 ( 
 datasetName 
 , 
  
 snapshotTableId 
 )) 
  
 . 
 build 
 (); 
  
  Job 
 
  
 job 
  
 = 
  
 bigquery 
 . 
  create 
 
 ( 
 JobInfo 
 . 
 of 
 ( 
 configuration 
 )); 
  
 job 
  
 = 
  
 job 
 . 
  waitFor 
 
 (); 
  
 if 
  
 ( 
 job 
 . 
  isDone 
 
 () 
 && 
 job 
 . 
 getStatus 
 (). 
 getError 
 () 
  
 == 
  
 null 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Undelete table recovered successfully." 
 ); 
  
 } 
  
 else 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
  
 "BigQuery was unable to copy the table due to an error: \n" 
  
 + 
  
 job 
 . 
 getStatus 
 (). 
 getError 
 ()); 
  
 return 
 ; 
  
 } 
  
 } 
  
 catch 
  
 ( 
  BigQueryException 
 
  
 | 
  
 InterruptedException 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Table not found. \n" 
  
 + 
  
 e 
 . 
 toString 
 ()); 
  
 } 
  
 } 
 } 
 

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 .

  // Import the Google Cloud client library 
 const 
  
 { 
 BigQuery 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/bigquery 
' 
 ); 
 const 
  
 bigquery 
  
 = 
  
 new 
  
  BigQuery 
 
 (); 
 async 
  
 function 
  
 undeleteTable 
 () 
  
 { 
  
 // Undeletes "my_table_to_undelete" from "my_dataset". 
  
 /** 
 * TODO(developer): Uncomment the following lines before running the sample. 
 */ 
  
 // const datasetId = "my_dataset"; 
  
 // const tableId = "my_table_to_undelete"; 
  
 // const recoveredTableId = "my_recovered_table"; 
  
 /** 
 * TODO(developer): Choose an appropriate snapshot point as epoch milliseconds. 
 * For this example, we choose the current time as we're about to delete the 
 * table immediately afterwards. 
 */ 
  
 const 
  
 snapshotEpoch 
  
 = 
  
 Date 
 . 
 now 
 (); 
  
 // Delete the table 
  
 await 
  
 bigquery 
 . 
 dataset 
 ( 
 datasetId 
 ). 
 table 
 ( 
 tableId 
 ). 
 delete 
 (); 
  
 console 
 . 
 log 
 ( 
 `Table 
 ${ 
 tableId 
 } 
 deleted.` 
 ); 
  
 // Construct the restore-from table ID using a snapshot decorator. 
  
 const 
  
 snapshotTableId 
  
 = 
  
 ` 
 ${ 
 tableId 
 } 
 @ 
 ${ 
 snapshotEpoch 
 } 
 ` 
 ; 
  
 // Construct and run a copy job. 
  
 await 
  
 bigquery 
  
 . 
 dataset 
 ( 
 datasetId 
 ) 
  
 . 
 table 
 ( 
 snapshotTableId 
 ) 
  
 . 
  copy 
 
 ( 
 bigquery 
 . 
 dataset 
 ( 
 datasetId 
 ). 
 table 
 ( 
 recoveredTableId 
 )); 
  
 console 
 . 
 log 
 ( 
  
 `Copied data from deleted table 
 ${ 
 tableId 
 } 
 to 
 ${ 
 recoveredTableId 
 } 
 ` 
 , 
  
 ); 
 } 
 

PHP

Before trying this sample, follow the PHP setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery PHP API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  use Google\Cloud\BigQuery\BigQueryClient; 
 /** 
 * Restore a deleted table from its snapshot. 
 * 
 * @param string $projectId The project Id of your Google Cloud Project. 
 * @param string $datasetId The BigQuery dataset ID. 
 * @param string $tableId Table ID of the table to delete. 
 * @param string $restoredTableId Table Id for the restored table. 
 */ 
 function undelete_table( 
 string $projectId, 
 string $datasetId, 
 string $tableId, 
 string $restoredTableId 
 ): void { 
 $bigQuery = new BigQueryClient(['projectId' => $projectId]); 
 $dataset = $bigQuery->dataset($datasetId); 
 // Choose an appropriate snapshot point as epoch milliseconds. 
 // For this example, we choose the current time as we're about to delete the 
 // table immediately afterwards 
 $snapshotEpoch = date_create()->format('Uv'); 
 // Delete the table. 
 $dataset->table($tableId)->delete(); 
 // Construct the restore-from table ID using a snapshot decorator. 
 $snapshotId = "{$tableId}@{$snapshotEpoch}"; 
 // Restore the deleted table 
 $restoredTable = $dataset->table($restoredTableId); 
 $copyConfig = $dataset->table($snapshotId)->copy($restoredTable); 
 $job = $bigQuery->runJob($copyConfig); 
 // check if the job is complete 
 $job->reload(); 
 if (!$job->isComplete()) { 
 throw new \Exception('Job has not yet completed', 500); 
 } 
 // check if the job has errors 
 if (isset($job->info()['status']['errorResult'])) { 
 $error = $job->info()['status']['errorResult']['message']; 
 printf('Error running job: %s' . PHP_EOL, $error); 
 } else { 
 print('Snapshot restored successfully' . PHP_EOL); 
 } 
 } 
 

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 .

  import 
  
 time 
 from 
  
 google.cloud 
  
 import 
  bigquery 
 
 # Construct a BigQuery client object. 
 client 
 = 
  bigquery 
 
 . 
  Client 
 
 () 
 # TODO(developer): Choose a table to recover. 
 # table_id = "your-project.your_dataset.your_table" 
 # TODO(developer): Choose a new table ID for the recovered table data. 
 # recovered_table_id = "your-project.your_dataset.your_table_recovered" 
 # TODO(developer): Choose an appropriate snapshot point as epoch 
 # milliseconds. For this example, we choose the current time as we're about 
 # to delete the table immediately afterwards. 
 snapshot_epoch 
 = 
 int 
 ( 
 time 
 . 
 time 
 () 
 * 
 1000 
 ) 
 # ... 
 # "Accidentally" delete the table. 
 client 
 . 
  delete_table 
 
 ( 
 table_id 
 ) 
 # Make an API request. 
 # Construct the restore-from table ID using a snapshot decorator. 
 snapshot_table_id 
 = 
 " 
 {} 
 @ 
 {} 
 " 
 . 
 format 
 ( 
 table_id 
 , 
 snapshot_epoch 
 ) 
 # Construct and run a copy job. 
 job 
 = 
 client 
 . 
  copy_table 
 
 ( 
 snapshot_table_id 
 , 
 recovered_table_id 
 , 
 # Must match the source and destination tables location. 
 location 
 = 
 "US" 
 , 
 ) 
 # Make an API request. 
  job 
 
 . 
 result 
 () 
 # Wait for the job to complete. 
 print 
 ( 
 "Copied data from deleted table 
 {} 
 to 
 {} 
 " 
 . 
 format 
 ( 
 table_id 
 , 
 recovered_table_id 
 ) 
 ) 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: