Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit be9b242

Browse files
authored
docs: add sample to delete job metadata (#798)
Planned to be included in https://cloud.google.com/bigquery/docs/managing-jobs
1 parent 46e65a6 commit be9b242

4 files changed

Lines changed: 102 additions & 17 deletions

File tree

samples/snippets/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ def dataset_id(bigquery_client: bigquery.Client, project_id: str):
5050
bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)
5151

5252

53+
@pytest.fixture(scope="session")
54+
def dataset_id_us_east1(bigquery_client: bigquery.Client, project_id: str):
55+
dataset_id = prefixer.create_prefix()
56+
full_dataset_id = f"{project_id}.{dataset_id}"
57+
dataset = bigquery.Dataset(full_dataset_id)
58+
dataset.location = "us-east1"
59+
bigquery_client.create_dataset(dataset)
60+
yield dataset_id
61+
bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)
62+
63+
64+
@pytest.fixture(scope="session")
65+
def table_id_us_east1(
66+
bigquery_client: bigquery.Client, project_id: str, dataset_id_us_east1: str
67+
):
68+
table_id = prefixer.create_prefix()
69+
full_table_id = f"{project_id}.{dataset_id_us_east1}.{table_id}"
70+
table = bigquery.Table(
71+
full_table_id, schema=[bigquery.SchemaField("string_col", "STRING")]
72+
)
73+
bigquery_client.create_table(table)
74+
yield full_table_id
75+
bigquery_client.delete_table(table, not_found_ok=True)
76+
77+
5378
@pytest.fixture
5479
def random_table_id(bigquery_client: bigquery.Client, project_id: str, dataset_id: str):
5580
"""Create a new table ID each time, so random_table_id can be used as

samples/snippets/delete_job.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2021 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+
16+
def delete_job_metadata(job_id: str, location: str):
17+
orig_job_id = job_id
18+
orig_location = location
19+
# [START bigquery_delete_job]
20+
from google.cloud import bigquery
21+
from google.api_core import exceptions
22+
23+
# TODO(developer): Set the job ID to the ID of the job whose metadata you
24+
# wish to delete.
25+
job_id = "abcd-efgh-ijkl-mnop"
26+
27+
# TODO(developer): Set the location to the region or multi-region
28+
# containing the job.
29+
location = "us-east1"
30+
31+
# [END bigquery_delete_job]
32+
job_id = orig_job_id
33+
location = orig_location
34+
35+
# [START bigquery_delete_job]
36+
client = bigquery.Client()
37+
38+
client.delete_job_metadata(job_id, location=location)
39+
40+
try:
41+
client.get_job(job_id, location=location)
42+
except exceptions.NotFound:
43+
print(f"Job metadata for job {location}:{job_id} was deleted.")
44+
# [END bigquery_delete_job]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2021 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+
import delete_job
18+
19+
20+
def test_delete_job_metadata(
21+
capsys, bigquery_client: bigquery.Client, table_id_us_east1: str
22+
):
23+
query_job: bigquery.QueryJob = bigquery_client.query(
24+
f"SELECT COUNT(*) FROM `{table_id_us_east1}`", location="us-east1",
25+
)
26+
query_job.result()
27+
assert query_job.job_id is not None
28+
29+
delete_job.delete_job_metadata(query_job.job_id, "us-east1")
30+
31+
out, _ = capsys.readouterr()
32+
assert "deleted" in out
33+
assert f"us-east1:{query_job.job_id}" in out

tests/system/test_client.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
from google.cloud import bigquery_v2
6464
from google.cloud.bigquery.dataset import Dataset
6565
from google.cloud.bigquery.dataset import DatasetReference
66-
from google.cloud.bigquery.schema import SchemaField
6766
from google.cloud.bigquery.table import Table
6867
from google.cloud._helpers import UTC
6968
from google.cloud.bigquery import dbapi, enums
@@ -506,22 +505,6 @@ def test_delete_dataset_delete_contents_false(self):
506505
with self.assertRaises(exceptions.BadRequest):
507506
Config.CLIENT.delete_dataset(dataset)
508507

509-
def test_delete_job_metadata(self):
510-
dataset_id = _make_dataset_id("us_east1")
511-
self.temp_dataset(dataset_id, location="us-east1")
512-
full_table_id = f"{Config.CLIENT.project}.{dataset_id}.test_delete_job_metadata"
513-
table = Table(full_table_id, schema=[SchemaField("col", "STRING")])
514-
Config.CLIENT.create_table(table)
515-
query_job: bigquery.QueryJob = Config.CLIENT.query(
516-
f"SELECT COUNT(*) FROM `{full_table_id}`", location="us-east1",
517-
)
518-
query_job.result()
519-
self.assertIsNotNone(Config.CLIENT.get_job(query_job))
520-
521-
Config.CLIENT.delete_job_metadata(query_job)
522-
with self.assertRaises(NotFound):
523-
Config.CLIENT.get_job(query_job)
524-
525508
def test_get_table_w_public_dataset(self):
526509
public = "bigquery-public-data"
527510
dataset_id = "samples"

0 commit comments

Comments
 (0)