This document is of type ABC" error persists after upgrading to 2.21.9

5 replies
Last updated: Nov 23, 2021
I’m wrestling with the “This document is of type ABC and cannot be edited as XYZ” bug with singletons on one of my sites. I upgraded to 2.21.9 from 2.21.4, but the bug persists. I’ve tried restoring the affected documents to a previous version, but I am unable. I’ve looked through as many Slack threads and GitHub Issues that I can find, but most of them end like two weeks ago, which suggests to me that the fix worked for many people.
Is there something I need to do to get the fix to kick in? Is there a way around this other than deleting the documents?
AI Update

I feel your pain with this singleton bug! Unfortunately, based on the information from the Sanity team, if a document was created or updated during the period when that regression was active, upgrading to version 2.21.9 alone won't automatically fix it. The document itself has corrupted _type metadata that persists even after the Studio fix.

The bad news is that these affected documents do need to be deleted and recreated. The good news is there are a couple of approaches to minimize the pain:

Option 1: Export, Edit, and Re-import (Recommended)

This approach from Sanity's support team lets you preserve your content:

  1. Export your dataset: sanity dataset export <dataset> <filename>.tar.gz
  2. Untar the file: tar -xzf <filename>.tar.gz (Mac/Linux)
  3. Open <folder-that-was-untarred>/data.ndjson in your editor
  4. Find the problem document (one per line) and change the _type to the correct value. Copy the _id
  5. Delete the document: sanity documents delete <_id> (add --dataset=<dataset> if needed)
  6. Repeat steps 4-5 for each affected document
  7. Create a new tarball: tar -czf <new-filename>.tar.gz <folder-that-was-untarred>
  8. Import with: sanity dataset import <new-filename>.tar.gz <dataset> --replace

Important: Test this on a non-production dataset first if possible!

Option 2: Use the HTTP API

You can also copy the document's JSON and create a new document with the correct _type using the HTTP Mutations API before deleting the old one. This gives you more control but requires writing some code.

The core issue is that the _type field in the stored document doesn't match what the Studio expects for that singleton, and there's no automatic migration path. The fix in 2.21.9 prevents new documents from getting corrupted, but doesn't heal existing ones.

If you have multiple affected documents, scripting the export/edit/import process would definitely save time over doing it manually!

Show original thread
5 replies
Hi User. Unfortunately, it sounds like a document created or updated while we experienced that regression will need to be deleted and recreated. You may be able to save some time by copying the document’s JSON and copying it to a new document with the API before deleting it. If you want help with that, please let me know.
Bummer. Thanks for letting me know. I’m not in front of my computer, but as long as you’re here, I would be grateful for a few pointers.
I just tested this approach and it seemed to work. It might be an option. Best to test on a non-production dataset first, if possible.
1. Export your dataset.
sanity dataset export <dataset> <filename>.tar.gz
2. Untar the file.
tar -xzf <filename>.tar.gz
on Mac/Linux.3. Open
<folder-that-was-untarred>/data.ndjson
in your editor. 4. Locate the problem document (there’s one document per line) and change the
_type
to the one desired. Copy the
_id
.5. In the terminal, run
sanity documents delete <_id>
. Add the
--dataset=<dataset>
flag if you need to work with your non-default dataset.6. Repeats steps 4-5 for as many documents that need to be changed. If there are more than a few, it’s probably faster to script this.
7. Close the editor and create a tarball from the entire folder with
tar -czf <new-filename>.tar.gz <folder-that-was-untarred>
. Don’t overwrite the old tarball as we want to keep that until we have confirmed everything went well.8. Import the new tarball with
sanity dataset import <new-filename>.tar.gz <dataset> --replace
.The original approach—copying the JSON and using the API to set it into a new document with the correct `_type`—is also an option.
Hi
user A
. I was finally able to get this squared away. Thanks for your help and for your clear instructions.
That’s great! I’m glad to hear it. Thanks for letting me know.

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?