How to change the _type of a document in Sanity.io using the terminal or Postman.
48 replies
Last updated: Feb 10, 2021
B
Is there a way to change
_typeof a document? I changed
namein the schema but it didn't change in the old documents and now I can't access them. I tried exporting, changing the
_typemanually and importing the new dataset but I get this error
The mutation(s) failed: Cannot modify immutable attribute "_type"
Feb 9, 2021, 11:23 PM
B
When I’ve needed to change the _type, I’ve used
sanity dataset exportto pull the data, edited the .ndjson file, then used
sanity dataset importto put everything back.
Feb 9, 2021, 11:33 PM
B
Tried it already but it says that "_type" is immutable
Feb 9, 2021, 11:34 PM
B
Sorry, I left out the important part. With the
--replaceflag.
Feb 9, 2021, 11:35 PM
B
This is what I get
Feb 9, 2021, 11:36 PM
B
Sorry, let me double-check what I did. I know I faced the same situation.
Feb 9, 2021, 11:37 PM
B
I could just change the id and import them and it would probably work but that would leave the old documents in the dataset.
Feb 9, 2021, 11:40 PM
B
Shoot. Sorry about this, Neycho. Yes, I definitely did what you just suggested—copied the documents of the old _type to new ones and then uploaded that. I suppose you could do that and then use the HTTP API to query the old `_type`s and delete them.
Feb 9, 2021, 11:44 PM
B
I’m going to try that now and will let you know how it goes.
Feb 9, 2021, 11:45 PM
B
This seems unnecessarily complex 😄 . Thank you for taking your time helping me.
Feb 9, 2021, 11:48 PM
B
My pleasure! Do you already have a write token? If not, and you want to do this too, you’ll need to set one at manage.sanity.io .
Feb 9, 2021, 11:49 PM
B
Just created one.
Feb 9, 2021, 11:50 PM
B
Okay, great. Do you use Postman or are you comfortable doing this in the terminal?
Feb 9, 2021, 11:51 PM
B
Well, neither but I will try with the terminal
Feb 9, 2021, 11:52 PM
B
k
Feb 9, 2021, 11:52 PM
B
Have you already uploaded your data with the new _types?
Feb 9, 2021, 11:52 PM
B
Yes
Feb 9, 2021, 11:52 PM
B
Okay. What is the old _type you want to delete?
Feb 9, 2021, 11:54 PM
B
productand
category
Feb 9, 2021, 11:55 PM
B
Okay, let’s do them one at a time.
Feb 9, 2021, 11:55 PM
B
And your dataset is production?
Feb 9, 2021, 11:56 PM
B
what do you mean by that? It's still in development
Feb 9, 2021, 11:57 PM
B
or do you mean its called production
Feb 9, 2021, 11:57 PM
B
Correct. The latter. Sorry.
Feb 9, 2021, 11:57 PM
B
It's called
biovitalis
Feb 9, 2021, 11:57 PM
B
Okay.
Feb 9, 2021, 11:58 PM
B
You’ll want to replace XXXXXXXX with your projectId and AUTH_TOKEN with your auth token you just created. Notice there’s a single quotation mark right after it.
Paste this in your terminal and press enter. You’ll get confirmation or an error.
curl --location --request POST '<https://XXXXXXXX.api.sanity.io/v1/data/mutate/biovitalis>' \ --header 'Authorization: Bearer AUTH_TOKEN' \ --header 'Content-Type: application/json' \ --data-raw '{ "mutations": [ { "delete": { "query": "*[_type == '\''product'\'']" } } ] } '
Feb 9, 2021, 11:58 PM
B
error
Feb 10, 2021, 12:00 AM
B
Multiple
Feb 10, 2021, 12:00 AM
B
Missing expression after unary operator '--'.
Feb 10, 2021, 12:00 AM
B
Hmmm… Okay, let’s try this:
curl --location --request POST '<https://XXXXXXXX.api.sanity.io/v1/data/mutate/test>' --header 'Authorization: <TOKEN>' --header 'Content-Type: application/json' --data-raw '{"mutations": [{"delete": {"query": "*[_type == '\''product'\'']"}}]}'
Feb 10, 2021, 12:04 AM
B
Same thing. Your token for <TOKEN>, your projectId for XXXXXXXX.
Feb 10, 2021, 12:05 AM
B
well, now I get this
Feb 10, 2021, 12:08 AM
B
oh, wait it says "test" not "biovitalis"
Feb 10, 2021, 12:09 AM
B
same error even after I changed it
Feb 10, 2021, 12:10 AM
B
should I just install Postman
Feb 10, 2021, 12:10 AM
B
I think you can replace
--location --requestwith
-X.
Feb 10, 2021, 12:10 AM
B
-X is --request and I don’t think --location is necessary.
Feb 10, 2021, 12:11 AM
B
You have a biovitalis dataset, but it’s empty.
Feb 10, 2021, 12:12 AM
B
If you go to https://manage.sanity.io/projects/ , select your project, then select Datasets, what do you see?
Feb 10, 2021, 12:13 AM
B
It should have 7 documents in it
Feb 10, 2021, 12:13 AM
B
Hmmm…
Feb 10, 2021, 12:13 AM
B
But hey I don't want to take more of your time than I already did. So I could probably handle this myself with some digging in the docs.
Feb 10, 2021, 12:14 AM
B
I don’t mind at all. Please do follow up if you get stuck.
Feb 10, 2021, 12:16 AM
B
(More than I’ve already gotten you stuck. 🥺)
Feb 10, 2021, 12:16 AM
B
Sure, thank you again. You are the MVP 🙌
Feb 10, 2021, 12:16 AM
B
Well, any progress is progress.
Feb 10, 2021, 12:17 AM
B
I was able to delete the documents. I downloaded Postman and it was pretty easy with it.
Feb 10, 2021, 12:55 AM
B
Happy to hear it! 😀
Feb 10, 2021, 12:56 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.