Is there any way to bulkify an insert/update operation performed from anonymous apex?

For instance, if I have a script that builds a list of 15,000 records that need to be updated, is there any way to avoid hitting the "Too many DML rows: 10001" error when trying to insert/update?

4 Answers 4


For a quick and dirty approach if I have more than 10,000 rows to update through anonymous apex I will sometimes do something like this

List<Thing__c> things = [select id from thing__c where status__c = 'Old value' limit 10000];
for (Thing__c thing : things) {
   // make changes
   thing.status__c = 'New Value';
update things;

Then run the script twice or whatever. This relies on the change you're making being something that you can filter in the where condition. Also this will get dull if there are many tens of thousands of records to update and you have to kick off the script again and again.

  • Thanks Doug. Given my time constraints, and my not really wanting to write a batchable class for this one-off data cleanup task, I ended up doing exactly what you suggested. Thanks!
    – BBP27
    Aug 10, 2013 at 16:18
  • 2
    @BBP27 You can also open multiple dev console/workbench instances and run jobs in parallel - I often do that if I need to generate 100k or so records in my scratch orgs.
    – dzh
    Apr 25, 2018 at 4:55
  • How would you identify which 10k records were returned in the query? And how would you ensure that the remaining 10k records would be returned the next time you run the script? Jan 13, 2020 at 19:15
  • 1
    @AyushGoyal each time you run the query you pick up some 10k records that match your criteria, and you update them. They no longer match the criteria, so they won't be selected when you run the query again.
    – Doug B
    Jan 17, 2020 at 11:22

One time I had to do this. My 7-year old spent about half an hour at my desk clicking the button repeatedly while I went downstairs and made a sandwich.

Still easier than writing and promoting a batch for a one time data cleanup.

  • 4
    awesome xD, I have to get a child xD Aug 7, 2015 at 7:19
  • 1
    Classic! what a great solution! Nov 4, 2015 at 17:00
  • 1
    Best instance of delegating work ever.
    – Richard N
    Dec 27, 2015 at 23:23

We can use Batch apex for these kinda problems. Please refer the below link for details



Hope it helps. Thanks


I wrote a Go library called batchforce to make scripting these types of updates easy without having to create a batch apex class.

It takes SOQL query and a function that maps an input record to update records. It uses the Bulk API to perform updates so it's pretty quick in most cases.

Here's the example from the README that updates the Title on all Contacts based on their Birthdate:

package main

import (
    batch "github.com/octoberswimmer/batchforce"
    force "github.com/ForceCLI/force/lib"

var (
    fiftyYearsAgo  = time.Now().AddDate(-50, 0, 0)
    thirtyYearsAgo = time.Now().AddDate(-30, 0, 0)

func main() {
    query := `
            Birthdate != null

    batch.Run("Contact", query, setTitle)

func setTitle(record force.ForceRecord) (updates []force.ForceRecord) {
    birthdate, err := time.Parse("2006-01-02", record["Birthdate"].(string))
    if err != nil {
    update := force.ForceRecord{}
    update["Id"] = record["Id"].(string)
    switch {
    case birthdate.Before(fiftyYearsAgo):
        update["Title"] = "Geezer"
        updates = append(updates, update)
    case birthdate.After(thirtyYearsAgo):
        update["Title"] = "Whippersnapper"
        updates = append(updates, update)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .