首页 > 解决方案 > 有没有办法一次清除多个表?

问题描述

我想清除存储在 bigquery 特定数据集中的表。

在控制台屏幕中,您不能一次删除多个表。

也无法在 bq CLI 中使用 * 进行删除。

有没有办法一次清除多个表?

标签: google-cloud-platformgoogle-bigquery

解决方案


虽然在文档中声明一次只能删除一个表,但可以使用 Python 脚本发出 API 请求,以删除数据集中的所有表。

我创建并测试了以下脚本:

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)))

我使用带有 Python 3 的 Jupyter Notebook 执行了上述代码。如果您在云 shell 环境中运行它,请确保安装所有依赖项pip install --upgrade google-cloud-bigquery


推荐阅读