Finding and removing unused fields in Sanity documents
11 replies
Last updated: Mar 17, 2023
J
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.
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.
Mar 16, 2023, 12:12 PM
H
It seems like you want to write a migration for your dataset: https://www.sanity.io/docs/migrating-data#0f6618dcaacf
Mar 16, 2023, 1:02 PM
D
If you expand that Developer info collapse, you’ll see just exactly the field in question
My example happens to be
My example happens to be
body(see attached)
Mar 16, 2023, 1:12 PM
J
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 migrateMar 16, 2023, 2:44 PM
J
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 schemaMar 16, 2023, 2:46 PM
H
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. 🙂
Mar 16, 2023, 2:46 PM
H
Ie, writing a migration that does that for every document you want to remove fields from.
Mar 16, 2023, 2:47 PM
H
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
filter out every field in the whitelist of allowed fields •
send the result into a patch of unsets
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
Mar 16, 2023, 2:52 PM
J
Wow, that is some nice out of the box thinking
Mar 16, 2023, 2:53 PM
J
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
Mar 16, 2023, 2:57 PM
D
I know in v2 you can get all the 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
schemainformation with
import schema from 'part:@sanity/base/schema'
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
contextargument in deskTool
structure
Mar 17, 2023, 12:48 PM
J
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
Mar 17, 2023, 4:45 PM
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.