Sub topic 9
Running Bulk API 2.0 query jobs
This chapter assumes you can write SOQL, have an authorized org and a way to get an access token, and can run curl in a terminal. Bulk API 2.0 is the right tool when an object is too large for a single synchronous query. You submit a query as a job, Salesforce processes it in the background, and you download the result as CSV.
How a query job works
A query job is asynchronous. You do not hold a connection open while Salesforce reads the data. Instead you create the job, check its state until it finishes, and then fetch the results.
The three requests
The commands below assume INSTANCE_URL and ACCESS_TOKEN are set in your shell. Replace JOB_ID with the id value returned by the first call.
curl -X POST "$INSTANCE_URL/services/data/v60.0/jobs/query" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"operation":"query","query":"SELECT Id, Name FROM Account","contentType":"CSV"}'
# Poll until the job state is JobComplete
curl "$INSTANCE_URL/services/data/v60.0/jobs/query/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN"
# Download the CSV results
curl "$INSTANCE_URL/services/data/v60.0/jobs/query/$JOB_ID/results" \
-H "Authorization: Bearer $ACCESS_TOKEN" > accounts.csvThe first call returns JSON that includes the job id and its state. The job moves through states such as UploadComplete and InProgress and ends in JobComplete. It can also end in Failed or Aborted, so a script should check for those states and stop rather than poll forever.
Including deleted records
The operation field controls which records the query returns. With query, you get only current records. With queryAll, the job also returns deleted records and archived records. For a backup that must capture records in the Recycle Bin, set the operation like this:
{"operation":"queryAll","query":"SELECT Id, Name, IsDeleted FROM Account","contentType":"CSV"}Selecting IsDeleted lets you tell live rows from deleted ones in the output file.
Limits and practical points
- Results are split across files for large queries. When a job returns a very large result set, the response carries a locator that you use to request the next set of results. Check the response headers for it, and keep requesting until no locator remains.
- Bulk API 2.0 queries count against your org's daily API allocations, but they use far fewer requests than paging through a REST query.
- Bulk API 2.0 query supports a restricted subset of SOQL. Relationship queries that return child records in a subquery are not supported, so export each child object with its own query and keep the parent
Idfield. - Access tokens expire. A long-running export script must refresh its token or fail cleanly.
Choosing it over the other methods
Use Data Loader or the Salesforce CLI for routine exports of moderate size. Move to Bulk API 2.0 when an object holds millions of rows, or when you are building your own export tool and need direct control over jobs and results.
Exercise
You need a backup file that includes records in the Recycle Bin. Which operation value do you set in the job request, and which extra field helps you identify those rows in the CSV?
Show answer
Set operation to queryAll, and include the IsDeleted field in the SELECT list. Rows with IsDeleted set to true are the deleted ones.