shiichan

Workflows createBatch() Levels Up: Create Up to 100 Instances by Count or List

Hi, Shii-chan here! Here's an update for anyone launching lots of Workflows!

Cloudflare Changelog developers.cloudflare.com

What was announced?

Cloudflare's Changelog announced a new way to call createBatch() in Workflows.

createBatch() now accepts an options object, so you can create up to 100 Workflow instances in one call. The result lists the created instances and explains why any others weren't created.

The story so far

createBatch() used to take an array. The announcement says that passing an array is now deprecated.

With the new form, the result tells you which entries weren't created and why.

What changes

There are two ways to create instances:

  • Pass count: create that many instances sharing the same options. IDs are generated
  • Pass instances: give each instance its own ID and options

Whether you want to fan out the same job many times or assign an ID per order, one call does it.

Also, IDs that already exist and IDs repeated within the batch are reported as errors instead of being skipped silently. Noticing dropped entries is a big plus!

Dive Deep

Using count:

const result = await env.MY_WORKFLOW.createBatch({
  count: 10,
  params: { report: "daily" },
});

Using instances:

const { created, errors } = await env.MY_WORKFLOW.createBatch({
  instances: [
    { id: "order-1", params: { orderId: 1 } },
    { id: "order-2", params: { orderId: 2 } },
  ],
});

for (const error of errors) {
  console.log(error.index, error.id, error.code, error.message);
}
  • created holds the instances that were created
  • errors holds each entry that wasn't created, identified by its position (index) in the input
  • Each error also carries id, code, and message

To use this form in local development and get its types from wrangler types, you need Wrangler 4.148.0 or later.

Wrap-up

  • createBatch() accepts an options object and creates up to 100 instances at once
  • count mass-produces identical instances, instances lets you customize each one
  • Entries that weren't created come back in errors with index, ID, code, and message
  • Existing or duplicate IDs become errors rather than silent skips
  • The array form is deprecated, and local development needs Wrangler 4.148.0 or later

If you do batch jobs or fan-out with Workflows, this one is worth a look!