David Masip
David Masip

Reputation: 2491

Run delete query from python in BigQuery

I'm using the following python code to delete some rows in a bigquery table:

from google.cloud import bigquery

bigquery_client = bigquery.Client(project='my-project')

query_delete = f"""
    delete from `my_table` where created_at >= '2021-01-01'
"""
print(query_delete)
# query_delete
job = bigquery_client.query(query)
job.result()
print("Deleted!")

However, the rows don't seem to be deleted when doing this from python. What am I missing?

Upvotes: 0

Views: 5694

Answers (2)

Sabil
Sabil

Reputation: 4510

I think below code snippet should work for you. You should pass query_delete instead of query

from google.cloud import bigquery

bigquery_client = bigquery.Client(project='my-project')

query_delete = f"""
  delete from `my_table` where created_at >= '2021-01-01'
"""

print(query_delete)
job = bigquery_client.query(query_delete)
job.result()
print("Deleted!")

Or you can try below formatted query

query_delete = (
  "DELETE from my_table "
  "WHERE created_at >= '2021-01-01'"
)

Upvotes: 2

y0j0
y0j0

Reputation: 3592

You probably need to enable standard SQL in BigQuery table.

https://stackoverflow.com/a/42831957/1683626

It should be set as default as is noted in official documentation, but maybe you changed it to legacy sql.

In the Cloud Console and the client libraries, standard SQL is the default.

Upvotes: 0

Related Questions