Updating schema and associated data in Sanity Studio

22 replies
Last updated: Jan 17, 2022
I've updated my schema within the github browser page (didn't have a copy locally), what do I need to do for the new schema to be used? I published it to the main branch.
AI Update

When you update your schema in GitHub and push it to the main branch, you need to deploy your Studio for those schema changes to take effect. Here's what you need to do:

  1. Pull the changes locally: First, pull your updated schema from GitHub to your local development environment:

    git pull origin main
  2. Deploy your Studio: Run the deployment command from your project directory:

    sanity deploy

    Or if you're using npm scripts:

    npm run deploy
    # or
    pnpm run deploy

The sanity deploy command does two important things:

  • Builds and deploys your Studio interface to your *.sanity.studio hosted URL
  • Deploys an up-to-date version of your schema types to Sanity's platform

This schema deployment is important because other parts of the Sanity Content Operating System (like AI Agent Actions and other features) rely on the deployed schema, not just the files in your repository.

If you're hosting your Studio on another platform like Vercel or Netlify instead of using sanity deploy, you'll need to trigger a rebuild on that platform. However, keep in mind that using sanity deploy is the recommended approach because it automatically handles schema deployment and integrates seamlessly with Sanity's platform features.

Note: If you're using GitHub Actions or another CI/CD pipeline, the deployment might happen automatically when you push to main, depending on your workflow configuration. Check if you have any automation set up in your .github/workflows directory.

I am accessing the deployed studio, then I realized I needed an additional piece of data - so I added it to the schema within the files on git. Am I understanding that I need to clone so that I can redeploy?
I am accessing the deployed studio, then I realized I needed an additional piece of data - so I added it to the schema within the files on git. Am I understanding that I need to clone so that I can redeploy?
Correct.
Thanks! What about all of the data I've created so far – it will still remain, right? I'm not removing any data pieces from the schema.
Correct! Your dataset is hosted separately from the Studio, so you can't accidentally overwrite it by redeploying the Studio.
perfect, thx
user M
I changed my schema, which included the exact name of the document types. Now all of my data is gone! Is there a way to change the schema and have the associated data update with it?
user M
I changed my schema, which included the exact name of the document types. Now all of my data is gone! Is there a way to change the schema and have the associated data update with it?
So, if you change your schema the content you've already created doesn't automatically update. It still exists, though! Your Studio just doesn't know how to display it since the schema changed. You'll need to perform a mutation on your content to update it. I prefer to use the JS client to write a script in cases like this.
Great! I knew you'd have a solution. is this the part of the page I need to pay attention to?
I'd use
patch
then
set
to alter existing docs?
Exactly, as well as
unset
if you need to remove a field.
Exactly, as well as
unset
if you need to remove a field.
perfect. thanks again!
one more question (and it feels like a dumb one): I'm not seeing in the docs about the JS client where I put this code, and how I run it. Do I create the script, add it to my HTML file, then load the page once to mutate the data?
Also, does the file need to be within the sanity folder to have access to the JS client I installed in there? Or should I have installed it in the (parent) project folder where my JS/HTML already exists?
user M
just checking if you saw this. Not to bug you too much...😬
I usually put it in my studio folder. Then you can run the script using
sanity exec <script-name-here> --withUserToken
Cool, thanks!! Is that somewhere in the documentation? If not, that should probably be added.
Thanks for the feedback! You can find info that particular command here . But it feels like a situation where you would have to already know what you're looking for. Someone new might not these things.
Ahh, yeah - I'd really recommend putting a link to this on the first page you sent me, since it's necessary to actually use the info on that first page. 😇
Thanks again!!
Actually, all of this JS client documentation seems like it's missing some bits of info, especially where the syntax is less similar to vanilla JS.
To double check: will this code rename all docs with _type: 'senator' into 'senators'? Do I need to add anything, or did I misunderstand how to interpret the docs?


const sanityClient = require('@sanity/client')
const client = sanityClient({
  projectId: '6dyl9k59',
  dataset: 'production',
  apiVersion: '2022-01-01', // use current UTC date - see "specifying API version"!
  token: 'skiq9pnWYIvHOlYZLz4P7Zg5dhl1zWhnq3Fc6GrtVnKTiy28SXm8XG0jTjD5KABRxh2jigsd418SuIjFbGwJDELksxZYkaTbCCgXkidnKazetbkBnUypEuIgIaZ3w3HHp8aXO6HlScrHB3mzzEHCS4Rk03MTUTsrbTTebWADYmLeeeFoAXRP', // or leave blank for unauthenticated usage
  useCdn: false, // `false` if you want to ensure fresh data
})


const namePatch = client.patch({ query: '*[_type == $type]', params: { type: 'senator' } }).set({_type: 'senators'});

namePatch();

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.

Was this answer helpful?