Cancel a job
Stay organized with collections
Save and categorize content based on your preferences.
Attempt to cancel a job.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, Node.js, and Python that demonstrate how to cancel a BigQuery job.\u003c/p\u003e\n"],["\u003cp\u003eEach code example requires setting up Application Default Credentials for authentication and points to relevant documentation for setup and reference.\u003c/p\u003e\n"],["\u003cp\u003eThe Go code sample uses the \u003ccode\u003ecancelJob\u003c/code\u003e function to issue a job cancellation request for a specified BigQuery job using \u003ccode\u003ejob.Cancel(ctx)\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe Java sample illustrates the cancellation process using the \u003ccode\u003ecancel()\u003c/code\u003e method on a job object, after creating a new job with \u003ccode\u003ebigquery.create()\u003c/code\u003e, obtaining it with \u003ccode\u003ebigquery.getJob()\u003c/code\u003e and checking if the operation was successful.\u003c/p\u003e\n"],["\u003cp\u003eThe node.js sample uses \u003ccode\u003ejob.cancel()\u003c/code\u003e to attempt to cancel a previously created job.\u003c/p\u003e\n"]]],[],null,["# Cancel a job\n\nAttempt to cancel a job.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Manage jobs](/bigquery/docs/managing-jobs)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Go API\nreference documentation](https://godoc.org/cloud.google.com/go/bigquery).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import (\n \t\"context\"\n \t\"fmt\"\n\n \t\"cloud.google.com/go/bigquery\"\n )\n\n // cancelJob demonstrates how a job cancellation request can be issued for a specific\n // BigQuery job.\n func cancelJob(projectID, jobID string) error {\n \t// projectID := \"my-project-id\"\n \t// jobID := \"my-job-id\"\n \tctx := context.Background()\n\n \tclient, err := bigquery.NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"bigquery.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tjob, err := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Client_JobFromID(ctx, jobID)\n \tif err != nil {\n \t\treturn nil\n \t}\n \treturn job.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Job_Cancel(ctx)\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobInfo.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html;\n import java.util.UUID;\n\n // Sample to cancel a job\n public class CancelJob {\n\n public static void main(String[] args) {\n // TODO(developer): Replace these variables before running the sample.\n String query = \"SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`\";\n cancelJob(query);\n }\n\n public static void cancelJob(String query) {\n try {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigquery = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.getDefaultInstance().getService();\n\n // Specify a job configuration to set optional job resource properties.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html queryConfig = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html.newBuilder(query).build();\n\n // The location and job name are optional,\n // if both are not specified then client will auto-create.\n String jobName = \"jobId_\" + UUID.randomUUID().toString();\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.html jobId = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.html.newBuilder().setLocation(\"us\").https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.Builder.html#com_google_cloud_bigquery_JobId_Builder_setJob_java_lang_String_(jobName).build();\n\n // Create a job with job ID\n bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_create_com_google_cloud_bigquery_DatasetInfo_com_google_cloud_bigquery_BigQuery_DatasetOption____(JobInfo.of(jobId, queryConfig));\n\n // Get a job that was just created\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html job = bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_getJob_com_google_cloud_bigquery_JobId_com_google_cloud_bigquery_BigQuery_JobOption____(jobId);\n if (job.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html#com_google_cloud_bigquery_Job_cancel__()) {\n System.out.println(\"Job canceled successfully\");\n } else {\n System.out.println(\"Job was not canceled\");\n }\n } catch (https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html e) {\n System.out.println(\"Job was not canceled.\\n\" + e.toString());\n }\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Node.js API\nreference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n // Import the Google Cloud client library\n const {BigQuery} = require('https://cloud.google.com/nodejs/docs/reference/bigquery/latest/overview.html');\n const bigquery = new https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html();\n\n async function cancelJob() {\n // Attempts to cancel a job.\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // const jobId = \"existing-job-id\";\n\n // Create a job reference\n const job = bigquery.https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html(jobId);\n\n // Attempt to cancel job\n const [apiResult] = await https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html.https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/job.html();\n\n console.log(apiResult.https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html.status);\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Python API\nreference documentation](/python/docs/reference/bigquery/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n\n\n def cancel_job(\n client: https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html,\n location: str = \"us\",\n job_id: str = \"abcd-efgh-ijkl-mnop\",\n ) -\u003e None:\n job = client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_cancel_job(job_id, location=location)\n print(f\"{https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.html.location}:{https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.html.job_id} cancelled\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]