
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! Backfilling content when adding new fields to your schema is a common scenario, and Sanity has a recommended approach for this using the CLI migration tool.
When you add a new field like subtitle to your blog schema, existing documents won't automatically have that field populated. Here's how to handle it:
The CLI migration tool is the recommended approach. It provides built-in abstractions, automatic batching to avoid rate limits, dry-run mode by default, and document validation against your schema.
Create a new migration:
npx sanity@latest migration createThis creates a migration file where you can use defineMigration to backfill your new field. For your subtitle example:
import {defineMigration, at, setIfMissing} from 'sanity/migrate'
export default defineMigration({
title: 'Add subtitle field to blog posts',
documentTypes: ['blog'], // Only target blog documents
migrate: {
document(doc, context) {
// Set a default value for subtitle if it doesn't exist
return at('subtitle', setIfMissing('Default subtitle'))
// Or derive it from another field:
// return at('subtitle', setIfMissing(doc.title?.substring(0, 50)))
}
}
})Run the migration:
# Dry run first (default) to see what would change
npx sanity migration run
# Then execute for real
npx sanity migration run --no-dry-runThe migration principles emphasize:
setIfMissing instead of set so you can safely run the script multiple times without overwriting manually updated valuesWhile the CLI migration tool is recommended, you can also:
sanity exec for more complex transformations with full programmatic controlFor adding a subtitle field to existing blog posts, you have several options:
setIfMissing('Add subtitle here') so editors know to update itsetIfMissing(doc.title?.substring(0, 50))The migration tool makes this straightforward and safe, with dry-run giving you confidence before touching production data!
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 operations
Content backend


The only platform powering content operations
By Industry


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