
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! Yes, you're absolutely right that batching your mutations into transactions is the way to go for patching 2,500 documents with 5,000 images. Let me break down the best practices:
Instead of individual API calls for each patch, you should group multiple mutations into transactions. This dramatically reduces your API usage and avoids rate limits. Here's the approach:
import {getCliClient} from 'sanity/cli'
const client = getCliClient()
// Batch your patches into transactions
const batchSize = 100 // Adjust based on your needs
const documents = [...] // Your 2,500 documents
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, patch =>
patch.set({ imageField: doc.uploadedImageReference })
)
})
await transaction.commit()
}Based on Sanity's documentation on transactions and bulk operations best practices:
Transaction size limit: Keep each transaction payload under 500kB. With 5,000 images across 2,500 documents, you'll likely want batches of 50-100 documents per transaction.
Rate limiting: Maximum 25 requests per second. Use a throttling library like p-throttle to manage this:
import pThrottle from 'p-throttle'
const throttle = pThrottle({
limit: 25,
interval: 1000 // 25 requests per second
})
const throttledCommit = throttle(async (transaction) => {
return await transaction.commit()
})p-limit), collect the asset references, then batch patch the documents. This is covered in the migration course on uploading assets performantly.For a cleaner approach, use Sanity's CLI migration tool which handles batching automatically:
npx sanity@latest migration createThe migration tool provides:
This approach follows general migration principles of idempotency and graceful error handling, which is crucial when dealing with thousands of documents.
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.
Content backend


The only platform powering content operations


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store