Reputation: 21
I want to clear the tables stored in a specific dataset of bigquery.
In the console screen, you cannot delete multiple tables at once.
It was also not possible to delete using the * in the bq CLI.
Is there a way to clear multiple tables at once?
Upvotes: 0
Views: 406
Reputation: 4042
While in the documentation it is stated that you can delete only one table at a time, it is possible to make an API request with a Python script in order to delete all the tables inside a dataset.
I created and tested the following script:
from google.cloud import bigquery
#construct BigQuery client object
client = bigquery.Client()
#select your dataset and list the tables within it
dataset_id='project_id.dataset'
tables = client.list_tables(dataset_id)
#inititalizing the list of tables
list_tables=[]
for table in tables:
#Create a list with the able reference for deletion 'project.dataset_id.table_id'
id =".".join([table.project,table.dataset_id,table.table_id])
list_tables.append(id)
#List of tables
print(list_tables)
#Delete all the tables inside the list of tables
for table in list_tables:
#print(table)
client.delete_table(table)
print("{} {}".format("Number of deleted tables in the dataset:", len(list_tables)))
I executed the above code using Jupyter Notebook with Python 3. If you run it in your cloud shell environment, make sure you install all the dependencies pip install --upgrade google-cloud-bigquery
.
Upvotes: 1
Reputation: 10069
I had similar situation and used the following Java code to delete the large no of tables.
Replace dataset and table_prefix values.
Set the service account JSON key file path as GOOGLE_APPLICATION_CREDENTIALS environment variable.
Code:
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
public class TableKiller {
String dataset = "MY_DATASET";
String table_prefix = "temp_table_";
public static void main(String[] args) throws ExecutionException, InterruptedException {
new TableKiller().deleteAll();
}
public void deleteAll() throws InterruptedException, ExecutionException {
ArrayList<String> tableNames = new ArrayList<>();
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
int i = 1;
bigQuery.listTables(dataset, BigQuery.TableListOption.pageSize(1000))
.iterateAll()
.forEach(table -> {
String tableName = table.getTableId().getTable();
if (tableName.startsWith(table_prefix)) {
tableNames.add(tableName);
System.out.println("Added " + tableName + i);
}
});
ForkJoinPool forkJoinPool = new ForkJoinPool(200);
forkJoinPool.submit(() -> tableNames
.parallelStream()
.forEach(this::deleteTable)).get();
}
private void deleteTable(String tableName) {
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
bigQuery.delete(TableId.of(dataset, tableName));
System.out.println("Deleted " + tableName);
}
}
Upvotes: 0