Is there any way to delete tables from the command line with a wildcard? Say I have a set of dated tables that I want to delete, with the same prefix - can I delete them all in one go, without writing my own shell script?
Asked
Active
Viewed 6,140 times
3 Answers
20
Not the nicest but you can generate a query to delete the tables via:
select concat("drop table ",table_schema,".", table_name, ";" )
from <insert_your_dataset_name>.INFORMATION_SCHEMA.TABLES
where table_name like "INSERT_YOUR_TABLE_NAME_%"
order by table_name desc
After running that click "Save Results", select "Copy to clipboard" from the dropdown.

Henry Munro
- 229
- 2
- 9
-
[table_catalog](https://cloud.google.com/bigquery/docs/information-schema-tables) collects the project id if anyone wants to know – Jonatas Eduardo Oct 25 '21 at 18:24
-
I find myself coming back to this post to copy & paste your snippet. This time I just want to thank you! – – caravana_942 Jun 22 '23 at 11:13
3
Nope, gotta script it. You can delete an entire dataset at once, but there's no way to use a wildcard to delete a subset of the tables in a dataset.

Jeremy Condit
- 6,766
- 1
- 28
- 30
2
Following @henry answer, I couldn't do it in sql (don't know why) so I adapted the concat to create a bunch of lines to feed all together to the shell
select concat("bq rm -f -t ",table_schema,".", table_name, ";" )
from <insert_your_dataset_name>.INFORMATION_SCHEMA.TABLES
where table_name like "INSERT_YOUR_TABLE_NAME_%"
order by table_name desc
Then just 'Save results' => ' Copy to clipboard' => open BQ Shell => paste!

Marco Rossi
- 231
- 1
- 6