12

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?

JasonB
  • 302
  • 1
  • 3
  • 13

3 Answers3

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