
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! Yes, there's definitely a better way than manually clicking through every article. You can use Sanity's migration tooling to bulk-generate slugs for all your existing articles.
First, generate a new migration using the CLI:
npx sanity@latest migration create "generate slugs for articles"This will create a migration file in your migrations directory. Open the generated index.ts file and replace it with something like this:
import {defineMigration, at, setIfMissing} from 'sanity/migrate'
// Helper function to create URL-friendly slugs
function slugify(text: string): string {
return text
.toLowerCase()
.replace(/[^\w\s-]/g, '') // Remove special characters
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/--+/g, '-') // Replace multiple hyphens with single
.trim()
}
export default defineMigration({
title: 'Generate slugs for articles',
documentTypes: ['article'], // Replace with your document type
migrate: {
document(doc, context) {
// Only generate if there's a title and slug is missing
if (doc.title && !doc.slug?.current) {
return [
at('slug', setIfMissing({
_type: 'slug',
current: slugify(doc.title)
}))
]
}
return []
}
}
})Before making any changes, run it in dry-run mode to see what would happen:
npx sanity migration run generate-slugs-for-articlesThis won't actually change anything - it just shows you what would be updated. Review the output to make sure it looks good!
Once you're happy with the dry run results, execute it for real:
npx sanity migration run generate-slugs-for-articles --dataset productionReplace production with your dataset name (often production or development).
The migration toolkit automatically handles batching, rate limits, and gives you progress feedback. The setIfMissing operation ensures you won't overwrite any slugs that already exist, so it's safe to run even if some articles already have slugs.
setIfMissing, so it won't overwrite existing slugs - only adds them where missingThis approach is way better than clicking through hundreds of articles! 😄
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