The Bulk API is the right answer to a specific question: how do I get millions of rows out of Salesforce quickly? It is so effective at this that developers reach for it when the task changes to "and the files too", and lose a day discovering that it has no way to return one. Knowing where that boundary is saves the day.
This article covers what a Bulk API export looks like in practice, the limits and the errors that come up most, and what to use for the binary content it leaves behind. For a broader walkthrough of the Export Wizard and Data Loader alongside the API, see our bulk data export guide.
What the Bulk API Is For
Standard REST and SOAP queries are synchronous. You ask, you wait, you receive up to 2,000 records, you ask for the next page. For ten million rows that is five thousand round trips.
Bulk API 2.0 is asynchronous. You submit a SOQL query as a job. Salesforce runs it in the background, and when it finishes you download the results as CSV in large pages. It uses far fewer API requests and is the mechanism behind Data Loader's Bulk mode and the Salesforce CLI's bulk commands.
A Bulk API 2.0 Export, Step by Step
1. Create the query job
POST /services/data/v62.0/jobs/query
Content-Type: application/json
{
"operation": "query",
"query": "SELECT Id, Name, Industry, CreatedDate FROM Account"
}
Use queryAll as the operation to include deleted and archived records.
2. Poll until it completes
GET /services/data/v62.0/jobs/query/750xx000000001AAA
Wait for "state": "JobComplete". Other end states are Failed and Aborted.
3. Download the results
GET /services/data/v62.0/jobs/query/750xx000000001AAA/results?maxRecords=50000
Accept: text/csv
If the response includes a Sforce-Locator header with a value other than null, request the next page by adding &locator=VALUE. Repeat until it is null.
The same thing from the CLI
sf data export bulk --query "SELECT Id, Name, Industry FROM Account" \
--output-file accounts.csv --wait 30 --target-org prod
And from Python
import time, requests
base = f"{instance_url}/services/data/v62.0/jobs/query"
headers = {"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"}
job = requests.post(base, headers=headers, json={
"operation": "query",
"query": "SELECT Id, Name, Industry FROM Account"}).json()
while True:
state = requests.get(f"{base}/{job['id']}", headers=headers).json()["state"]
if state in ("JobComplete", "Failed", "Aborted"):
break
time.sleep(10)
locator, part = None, 0
while state == "JobComplete":
params = {"maxRecords": 50000}
if locator:
params["locator"] = locator
r = requests.get(f"{base}/{job['id']}/results",
headers={**headers, "Accept": "text/csv"}, params=params)
with open(f"accounts_{part}.csv", "wb") as f:
f.write(r.content)
locator = r.headers.get("Sforce-Locator")
part += 1
if not locator or locator == "null":
break
Obtaining access_token and instance_url is a separate OAuth step. The JWT bearer or client credentials flow suits unattended scripts.
What Bulk Queries Do Not Support
| Limitation | What you see | Workaround |
|---|---|---|
| Compound fields (address, geolocation) | Selecting compound data not supported in Bulk Query | Select the component fields: BillingStreet, BillingCity, and so on. For geolocation, Field__Latitude__s and Field__Longitude__s. |
| Parent-to-child subqueries | Query rejected | Query the child object separately with the parent ID and join the CSVs. |
| Aggregates and GROUP BY | Query rejected | Use a normal REST query for counts and sums. |
| Binary (base64) field content | No file data in the CSV | Fetch each binary through the REST blob endpoint. See below. |
| Long-running queries | Job fails after repeated timeouts | Add a selective filter, or split by CreatedDate or ID range. |
SELECTing BillingAddress, ShippingAddress, MailingAddress or a custom geolocation field, often because a tool generated a "select all fields" query. Remove the compound field and list its parts.Limits to Plan Around
- Daily query jobs and result volume. Bulk API 2.0 has its own rolling 24-hour caps on the number of query jobs and total result size. They are generous for normal use, and are documented per API version.
- Result retention. Job results are kept for seven days. Download them promptly.
- Standard API allocation. Creating jobs, polling and fetching results each count as API requests against the org's daily total, though far fewer than a synchronous export would use.
- Visibility. The job returns only what the running user can see.
A consolidated list of export limits is in Salesforce data export limits.
Why Files Are Different
You can run a bulk query against ContentVersion or Attachment, and it is a good way to build an inventory:
SELECT Id, ContentDocumentId, Title, FileExtension, ContentSize, Checksum
FROM ContentVersion WHERE IsLatest = true
What comes back is a row per file. The content, held in VersionData, is not part of it. The Bulk API's result format is CSV, and Salesforce does not serialise gigabyte binaries into it. The bytes are served only by the REST API, one file per request:
GET /services/data/v62.0/sobjects/ContentVersion/{Id}/VersionData
GET /services/data/v62.0/sobjects/Attachment/{Id}/Body
GET /services/data/v62.0/sobjects/Document/{Id}/Body
Building the File Half Yourself
The inventory query plus a loop over the blob endpoint is a working prototype in an afternoon. Turning it into something you would trust with a migration means adding:
- Token refresh for runs that outlast a session
- Retry with back-off on timeouts and
503responses - Detection of
REQUEST_LIMIT_EXCEEDED, with pause and resume - Streaming to disk, so a 2 GB file does not sit in memory
- Resolving
ContentDocumentLinkto place each file under its parent record, including files shared with several records. See the Files data structure. - Sanitising file names and handling duplicates
- A checkpoint, so a restart does not begin again
- A log that can be reconciled against the inventory
- Separate handling for Attachments, Documents, Classic Notes and Enhanced Notes
If you have the developer time and will maintain it, that is a reasonable project. If the export is a one-off or the team has no developer to own it, a ready-made tool is cheaper.
The Practical Split
| Data | Mechanism | Tool |
|---|---|---|
| Object records, any volume | Bulk API 2.0 query | Salesforce CLI, Data Loader in Bulk mode, or a script |
| File and attachment inventory | Bulk API 2.0 query | Same |
| File, attachment, document and note content | REST blob endpoints, one request per file | SFDC File Exporter, or a script you maintain |
SFDC File Exporter is a desktop app that implements the third row: it resolves the links, downloads each binary, pauses at the API limit, skips files already downloaded on restart, and writes everything into folders by object and record with a job history. It accepts a SOQL filter or a CSV of record IDs, so the output of your bulk query can drive it directly.
Frequently Asked Questions
Can the Salesforce Bulk API export files and attachments?
No. Bulk API query jobs return record data as CSV. They do not return the binary content held in ContentVersion.VersionData, Attachment.Body or Document.Body. You can bulk export the metadata rows for those objects, and then each file's content has to be downloaded individually through the REST API.
What is the fastest way to export large Salesforce datasets?
For records, a Bulk API 2.0 query job, split by a date or ID range and run in parallel where the object is very large. The work is done asynchronously on Salesforce's servers and results come back in large CSV chunks, which is far faster than paging through the REST or SOAP API.
How do I fix 'Selecting compound data not supported in Bulk Query'?
Remove compound fields from the SELECT list and query their components instead. Replace BillingAddress with BillingStreet, BillingCity, BillingState, BillingPostalCode and BillingCountry, and replace a geolocation field with its latitude and longitude fields, for example Location__Latitude__s and Location__Longitude__s.
How do I export Salesforce data in bulk with Python?
Authenticate with OAuth, create a Bulk API 2.0 query job by POSTing your SOQL to /services/data/vXX.0/jobs/query, poll the job until its state is JobComplete, then download the CSV results page by page using the Sforce-Locator header. The simple-salesforce library wraps these calls if you prefer not to write them by hand.
What are the Bulk API query limits?
Salesforce documents a daily cap on the number of query jobs and on total result size per rolling 24 hours, and a query that runs too long is failed. The figures have changed between releases, so check the Bulk API limits page for your API version. They are separate from, and in addition to, the org's standard daily API request allocation.
Use Each API for What It Does
Bulk API for rows, REST for bytes. Projects go wrong when one is expected to do the other's job. Run your bulk queries for the record data and the file inventory, then hand the binaries to something built for them. Download SFDC File Exporter and try 50 files on a sandbox before deciding whether to write that loop yourself.