Bulk API 2.0 Query Delete

In this post, the Salesforce Bulk API 2.0 query will b used to delete some records. The Bulk query jobs allow asynchronous processing of SOQL queries because they are designed to handle queries that return large amounts of data (that is 10,000 records or more). The queries in Bulk API 2.0. support the following URIs and methods all of which will be used in this post except PATCH:
URI: /services/data/vXX.X/jobs/query
Description: Creates a query job.

URI: /services/data/vXX.X/jobs/query
HTTP Method: GET
Description: Gets information about all query jobs in the org.

URI: /services/data/vXX.X/jobs/query/queryJobId
HTTP Method: GET
Description: Gets information about one query job.

URI: /services/data/vXX.X/jobs/query/queryJobId/results
HTTP Method: GET
Description: Gets the results for a query job.

URI: /services/data/vXX.X/jobs/query/queryJobId
Description: Aborts a query job.

URI: /services/data/vXX.X/jobs/query/queryJobId
Description: Deletes a query job.

Create a Query Job

Just like my post on Bulk Insert Records Using Bulk API 2.0, to delete records using Bulk API 2.0 query, Workbench (use your preference) will be used to create a job
(1) Log in to your Trailhead Playground or Developer Org and navigate to Workbench.
(2) On the Workbench page, for Environment, select Production. For API Version, select the highest available number from the dropdown. Check the checkbox to select I agree to the terms of service.
(3) Click Login with Salesforce.
(4) In the top menu, hover over utilities and select REST Explorer.
(5) For URI, use the first URI on this page – /services/data/v51.0/jobs/query (where v51.0 is the highest number now)
(6) Select POST for the method and for the Request Body, use the one below
“operation”: “query”,
“query”: “Select Id, Lastname from Contact where createdDate > LAST_WEEK”

(I added the where clause to achieve my use case because I only want to delete records created recently in the org.
(7) Click Execute
You should get a response like this:

Note the queryJobId: 7503h00000AELqVAAX

Monitor the State of the Job

zTo monitor the state of the job:
(1) Create a GET request.
(2) For URI, use /services/data/v51.0/jobs/query/7503h00000AELqVAAX
(where 7503h00000AELqVAAX is the job ID that was returned by the POST request that created the query job )
You get a response that shows the state of the job – JobComplete:

Note the number of records processed. Check this to make sure it is the expected number

Get The Result Of The Job

To get the results of the job, create a GET request with the URI below
/services/data/v51.0/jobs/query/7503h00000AELqVAAX/results (replace the jobId with your generated one)
Below is the results


Delete the Query Job

Note: Before a job can be deleted, its state should be  JobComplete, Aborted, or Failed.
To delete the query job,
(1) For the method , select DELETE
(2) URI: /services/data/v51.0/jobs/query/7503h00000AELqVAAX
(3) Click Execute, the outcome should be like this:



Bulk API 2.0 and Bulk API Developer Guide