🗓️ Everything *[NYC] is back. A free gathering for AI builders. Sept 9 →

Batch patch commits to multiple documents via API

19 repliesLast updated: Nov 29, 2025

is it possible to run a patch commit to a set of documents at once?We want to update the same type of information to many documents at once via the api.
AI Update

Yes, you can definitely apply patch operations to multiple documents at once! Sanity provides several approaches depending on your specific needs:

Using Transactions

The most straightforward way to patch multiple documents atomically is using transactions. Transactions allow you to batch multiple patch operations together with all-or-nothing execution:

await client
  .transaction()
  .patch('doc-id-1', p => p.set({ status: 'published' }))
  .patch('doc-id-2', p => p.set({ status: 'published' }))
  .patch('doc-id-3', p => p.set({ status: 'published' }))
  .commit()

This ensures either all patches succeed or none are applied, maintaining data consistency.

Bulk Updates with Batching

For updating many documents (100+), you'll want to batch your operations and respect rate limits. Here's a pattern that works well:

import pThrottle from 'p-throttle';

const throttle = pThrottle({
  limit: 25,  // Max 25 requests per second
  interval: 1000
});

// Query for documents you want to update
const documents = await client.fetch('*[_type == "post" && status == "draft"]');

// Process in batches
const batchSize = 100;
for (let i = 0; i < documents.length; i += batchSize) {
  const batch = documents.slice(i, i + batchSize);
  
  const transaction = client.transaction();
  batch.forEach(doc => {
    transaction.patch(doc._id, p => p.set({ status: 'published' }));
  });
  
  await throttle(() => transaction.commit())();
}

Important Considerations

The patches documentation provides more details on available patch operations like set, unset, insert, and array manipulation.

Show original thread
19 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions