|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from google.cloud import bigquery |
| 16 | + |
| 17 | + |
| 18 | +def delete_label_table(table_id: str, label_key: str) -> bigquery.Table: |
| 19 | + orig_table_id = table_id |
| 20 | + orig_label_key = label_key |
| 21 | + # [START bigquery_delete_label_table] |
| 22 | + from google.cloud import bigquery |
| 23 | + |
| 24 | + client = bigquery.Client() |
| 25 | + |
| 26 | + # TODO(dev): Change table_id to the full name of the table you wish to delete from. |
| 27 | + table_id = "your-project.your_dataset.your_table_name" |
| 28 | + # TODO(dev): Change label_key to the name of the label you want to remove. |
| 29 | + label_key = "color" |
| 30 | + # [END bigquery_delete_label_table] |
| 31 | + table_id = orig_table_id |
| 32 | + label_key = orig_label_key |
| 33 | + # [START bigquery_delete_label_table] |
| 34 | + table = client.get_table(table_id) # API request |
| 35 | + |
| 36 | + # To delete a label from a table, set its value to None |
| 37 | + table.labels[label_key] = None |
| 38 | + |
| 39 | + table = client.update_table(table, ["labels"]) # API request |
| 40 | + |
| 41 | + print(f"Deleted label '{label_key}' from {table_id}.") |
| 42 | + # [END bigquery_delete_label_table] |
| 43 | + return table |
0 commit comments