Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to change the _type of a document in Sanity.io using the terminal or Postman.

48 replies
Last updated: Feb 10, 2021
Is there a way to change
_type
of a document? I changed
name
in the schema but it didn't change in the old documents and now I can't access them. I tried exporting, changing the
_type
manually 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
When I’ve needed to change the _type, I’ve used
sanity dataset export
to pull the data, edited the .ndjson file, then used
sanity dataset import
to put everything back.
Feb 9, 2021, 11:33 PM
Tried it already but it says that "_type" is immutable
Feb 9, 2021, 11:34 PM
Sorry, I left out the important part. With the
--replace
flag.
Feb 9, 2021, 11:35 PM
This is what I get
Feb 9, 2021, 11:36 PM
Sorry, let me double-check what I did. I know I faced the same situation.
Feb 9, 2021, 11:37 PM
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
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
I’m going to try that now and will let you know how it goes.
Feb 9, 2021, 11:45 PM
This seems unnecessarily complex 😄 . Thank you for taking your time helping me.
Feb 9, 2021, 11:48 PM
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
Just created one.
Feb 9, 2021, 11:50 PM
Okay, great. Do you use Postman or are you comfortable doing this in the terminal?
Feb 9, 2021, 11:51 PM
Well, neither but I will try with the terminal
Feb 9, 2021, 11:52 PM
k
Feb 9, 2021, 11:52 PM
Have you already uploaded your data with the new _types?
Feb 9, 2021, 11:52 PM
Yes
Feb 9, 2021, 11:52 PM
Okay. What is the old _type you want to delete?
Feb 9, 2021, 11:54 PM
product
and
category
Feb 9, 2021, 11:55 PM
Okay, let’s do them one at a time.
Feb 9, 2021, 11:55 PM
And your dataset is production?
Feb 9, 2021, 11:56 PM
what do you mean by that? It's still in development
Feb 9, 2021, 11:57 PM
or do you mean its called production
Feb 9, 2021, 11:57 PM
Correct. The latter. Sorry.
Feb 9, 2021, 11:57 PM
It's called
biovitalis
Feb 9, 2021, 11:57 PM
Okay.
Feb 9, 2021, 11:58 PM
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.

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'\'']"
      }
    }
  ]
}
'
Paste this in your terminal and press enter. You’ll get confirmation or an error.
Feb 9, 2021, 11:58 PM
error
Feb 10, 2021, 12:00 AM
Multiple
Feb 10, 2021, 12:00 AM
Missing expression after unary operator '--'.
Feb 10, 2021, 12:00 AM
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
Same thing. Your token for &lt;TOKEN&gt;, your projectId for XXXXXXXX.
Feb 10, 2021, 12:05 AM
well, now I get this
Feb 10, 2021, 12:08 AM
oh, wait it says "test" not "biovitalis"
Feb 10, 2021, 12:09 AM
same error even after I changed it
Feb 10, 2021, 12:10 AM
should I just install Postman
Feb 10, 2021, 12:10 AM
I think you can replace
--location --request
with
-X
.
Feb 10, 2021, 12:10 AM
-X is --request and I don’t think --location is necessary.
Feb 10, 2021, 12:11 AM
You have a biovitalis dataset, but it’s empty.
Feb 10, 2021, 12:12 AM
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
It should have 7 documents in it
Feb 10, 2021, 12:13 AM
Hmmm…
Feb 10, 2021, 12:13 AM
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
I don’t mind at all. Please do follow up if you get stuck.
Feb 10, 2021, 12:16 AM
(More than I’ve already gotten you stuck. 🥺)
Feb 10, 2021, 12:16 AM
Sure, thank you again. You are the MVP 🙌
Feb 10, 2021, 12:16 AM
Well, any progress is progress.
Feb 10, 2021, 12:17 AM
I was able to delete the documents. I downloaded Postman and it was pretty easy with it.
Feb 10, 2021, 12:55 AM
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.

Was this answer helpful?