Finding and removing unused fields in Sanity documents

11 replies
Last updated: Mar 17, 2023
We have an unknown number of documents with fields that were once in a schema, but not anymore. When such a document is opened in Studio, it displays “unknown field found” with the option to reset the field.Is there any way to find these unknown field types using a query or some sort of script? Opening every document one by one is not feasible to do.
Since these fields count towards our attribute limit, we want to get rid of any unused ones.
Basically what I seem to be looking for is a way to validate an entire dataset against a schema.
It seems like you want to write a migration for your dataset: https://www.sanity.io/docs/migrating-data#0f6618dcaacf
If you expand that Developer info collapse, you’ll see just exactly the field in question
My example happens to be
body
(see attached)
Thanks
user C
, however the fields have been renamed/undefined some time ago and I don’t have their names anymore, so I can’t specify which ones to remove or migrate
user G
yes, I also have that, but that would mean I would have to randomly open documents to find an unknown field, then run a script to remove it. And keep doing that until I can no longer find any fields.It is currently my plan of attack, but there must be a better way to validate all documents against a schema
You could go for a whitelist instead. If you know every field you want to keep ( the ones in the schema ), you make a list of them and then create sort-of new documents that only contain the whitelisted fields. I think that might work, without having tested it in detail. 🙂
Ie, writing a migration that does that for every document you want to remove fields from.
In practice, since you have to explicitly unset fields when using the sanity client, you would probably have to, for each document:
get the documents current fields using
Object.keys()

filter out every field in the whitelist of allowed fields
send the result into a patch of unsets
Wow, that is some nice out of the box thinking
I was hoping that there would be some kind of
schema.validateDocument(doc)
function in https://github.com/sanity-io/sanity/tree/next/packages/%40sanity/schema
I know in v2 you can get all the
schema
information with
import schema from 'part:@sanity/base/schema'
I used to create a plugin that where I will get all the fields I have in the current schema and loop through all my documents to compare which fields are present and what/not. Then I do a patch!
Problem is now that in v3, I don’t know what’s the equivalent of that import. I came to know ``import {useSchema} from 'sanity'`` but that is only applicable inside a React component while the other example is with `sanity.config.ts``s
context
argument in deskTool
structure
Makes me glad we haven’t migrated to v3 just yet 🙂Is that plugin code still somewhere? would save me some time trying to figure this out myself

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?