Sub topic 11
Batch size and Bulk API settings
This chapter explains how Data Loader splits a CSV file into batches, how the SOAP API and Bulk API differ, and which settings to change when a load is slow or fails with lock errors. It assumes you can already run insert and update operations in Data Loader and read success and error files. It also assumes you know that Apex triggers, flows, and validation rules run when records are saved.
What a batch is
Data Loader does not send your file as one request. It reads the CSV, cuts it into groups of rows, and sends each group to Salesforce as a unit of work called a batch. The batch size is the number of rows in each group.
Batch size matters for three reasons:
- Speed. Each batch has a fixed overhead. Larger batches mean fewer round trips.
- Limits. Salesforce caps how many API calls or batches you can use in a period. Fewer, larger batches use less of that allowance.
- Failure behavior. A trigger that is too heavy, or a lock conflict, tends to show up when many records are processed together.
Data Loader can use two APIs, and each handles batches differently.
| Setting | SOAP API (default) | Bulk API | Bulk API 2.0 |
|---|---|---|---|
| Processing | Synchronous. Data Loader waits for each batch. | Asynchronous. Salesforce queues batches and Data Loader polls for status. | Asynchronous. Salesforce chooses the batch boundaries. |
| Maximum batch size | 200 records | 10,000 records | Not set by you |
| Best for | Small and medium files, debugging | Large files, from tens of thousands to millions of rows | Large files when you want fewer settings |
| Parallel or serial | Sequential from the client | Parallel by default, serial optional | Managed by Salesforce |
Bulk API 2.0 support depends on your Data Loader version. Recent versions include it as an option in the Settings dialog. Older versions offer only SOAP and Bulk API. Check your version's Settings dialog before relying on it.
Where the settings live
In Data Loader, open Settings from the menu. The options below are the ones that affect throughput and failures. Exact labels vary slightly between versions.
- Batch size. Rows per batch. The maximum is 200 for SOAP and 10,000 for Bulk API.
- Use Bulk API. Sends batches through the Bulk API instead of SOAP.
- Enable serial mode for Bulk API. Processes Bulk API batches one at a time instead of in parallel.
- Timeout. How many seconds Data Loader waits for a response before treating a call as failed.
- Query request size. For exports. It sets how many rows each query fetch returns. The default is 500 and the maximum is 2,000.
- Compression. Compresses the request and response, which helps on slow networks.
- Insert null values. Controls whether empty CSV cells clear the field. This is not a performance setting, but it changes what a batch does to existing data, so confirm it before large updates.
The settings are saved in a config.properties file in Data Loader's configuration folder. The command-line version reads the same keys, which the last chapter in this tutorial covers. A relevant excerpt looks like this:
# Use the Bulk API with a large batch size
sfdc.useBulkApi=true
sfdc.loadBatchSize=5000
# Process batches one at a time to avoid lock contention
sfdc.bulkApiSerialMode=true
# Export fetch size
sfdc.extractionRequestSize=2000
# Seconds to wait for a response
sfdc.timeoutSecs=600How a Bulk API load flows
With the Bulk API, Data Loader uploads batches, Salesforce processes them in the background, and Data Loader then retrieves results. A job is the container for all batches in one operation.
Each batch succeeds or fails per record. If row 37 in a batch of 200 fails validation, the other 199 rows still save, and row 37 appears in the error file. This matches what you saw with SOAP loads.
Estimating the number of batches
Before a large load, estimate how many batches you will create. This tells you whether the job fits within your daily limits and how long it may take. The script below reads a CSV file and prints the batch count for a given size. It uses only the Python standard library.
import csv
import math
import sys
def count_batches(path: str, batch_size: int) -> tuple[int, int]:
"""Return (row_count, batch_count) for a CSV file with a header row."""
with open(path, newline="", encoding="utf-8") as f:
reader = csv.reader(f)
next(reader, None) # skip header
rows = sum(1 for _ in reader)
return rows, math.ceil(rows / batch_size) if rows else 0
if __name__ == "__main__":
file_path = sys.argv[1]
size = int(sys.argv[2])
rows, batches = count_batches(file_path, size)
print(f"{rows} rows -> {batches} batches of up to {size}")Run it as python count_batches.py accounts.csv 200. For a 1,000,000-row file, a batch size of 200 produces 5,000 batches. A batch size of 10,000 produces 100. Both are valid, but the second uses far less of the batch allowance and has less overhead. Salesforce publishes the daily Bulk API batch and record limits, and they change by edition and release, so check the current limits for your org.
Choosing a batch size
Bigger is faster only until something in your org objects. Use these rules as a starting point.
- Under about 5,000 rows, use SOAP at the default. The time saved by Bulk API is small, and SOAP gives you simpler error behavior.
- For tens of thousands of rows or more, use Bulk API. Start with a batch size of a few thousand and adjust.
- When the object has heavy automation, lower the batch size. Triggers, flows, and processes run in chunks of up to 200 records inside each transaction. A very large batch simply means many chunks, but a single chunk that exceeds CPU time or query limits will fail every record in it.
- When debugging, reduce the batch size to 1 or a small number. The error message then points to exactly one record, and one bad row can't affect its neighbors.
Batch size and automation
Two records in the same batch can interact. A trigger that queries the database once per record, or a flow that updates related records, uses limits that are shared across the whole chunk of 200. Data that works in a 10-row test can fail at 200 for this reason. Always test with a realistic batch from a sandbox, not just a few rows.
Parallel and serial mode
In its default parallel mode, the Bulk API processes several batches at the same time. This is fast, but two batches may try to update the same record or the same parent at once. Salesforce locks a record while it is being written, so the second batch fails with UNABLE_TO_LOCK_ROW.
Common causes are:
- Many child records pointing to the same parent, such as thousands of Contacts for a few Accounts. Salesforce locks the parent while saving each child.
- Roll-up summary fields on the parent.
- Sharing recalculation, for example when ownership changes on records in an account hierarchy.
- Triggers or flows that update a shared record, such as a counter.
The fixes, in order of preference:
- Sort the CSV by the parent ID so that rows for the same parent sit in the same batch rather than across several parallel ones.
- Enable serial mode. Batches then run one at a time and the conflict disappears. The cost is speed.
- Reduce the batch size if timeouts accompany the lock errors.
- Re-run only the failed rows from the error file after the main job completes.
Timeouts and failed batches
A batch can fail as a whole, which differs from a row failure.
- Client timeout. If Data Loader doesn't get a response within the Timeout setting, it reports an error for that call. Raise the timeout for slow networks or very wide records, and use compression.
- Bulk batch processing time. A Bulk API batch that runs for too long is stopped and retried by Salesforce a limited number of times, and then marked failed. A smaller batch size for a heavily automated object shortens each batch.
- Request size. Very large field values, such as long text areas or file data, make a batch heavy even when the row count is low. Use fewer rows per batch for these.
When a whole batch fails, every row in it appears in the error file with the same message. Look for the same message repeated across a block of consecutive rows. That pattern signals a batch problem, not bad data.
Export settings
Exports use a different setting. For queries, Query request size sets how many rows Data Loader fetches per round trip. A value of 2,000 is the maximum and usually the fastest for large exports. If you export a very wide object and see timeouts, lower it. With Bulk API enabled, exports can also run through the Bulk API, which is the better choice for millions of rows.
A tuning checklist
- Confirm the load works on 10 to 20 rows in a sandbox.
- Estimate batches with the script above.
- Choose SOAP or Bulk API from the row count.
- Set the batch size. Lower it for heavy automation.
- Run a representative sample of a few thousand rows.
- Read the error file for lock or limit messages.
- Switch to serial mode only if lock errors appear.
- Run the full load, then re-run the error file.
Exercise
You load 400,000 Contact rows with the Bulk API in parallel mode. Many rows fail with UNABLETOLOCK_ROW, and most of the failing contacts belong to a few large Accounts. Name two changes that reduce these errors and say what each costs.
Show answer
Sort the CSV by AccountId so that contacts for the same Account fall in the same batch, which reduces parallel batches competing for the same parent lock. This costs only a sort step. Alternatively, enable serial mode so batches run one at a time, which removes the conflict but makes the load slower. You can also reload only the failed rows from the error file in serial mode.
Exercise
A file has 95,000 rows. How many batches does Data Loader create at a batch size of 200, and at a batch size of 10,000? Which API allows each size?
Show answer
At 200 rows per batch, it creates 475 batches (95,000 / 200 = 475). At 10,000 rows per batch, it creates 10 batches (95,000 / 10,000 = 9.5, rounded up to 10). A size of 200 is the maximum for the SOAP API, and Bulk API allows up to 10,000.