👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Troubleshooting removing a deprecated field from a dataset in Sanity.io.

22 replies
Last updated: Apr 16, 2024
Hi,I'm looking for a efficient way to remove a deprecated field from a Dataset. I have tried executing this
script and tried with a migration script with an unset(). I not having any success atm. What is the best practice when you want to batch remove a field from a dataset.
Apr 15, 2024, 8:41 PM
That script may be giving you problems because it’s still using V2 syntax. That said, it’s overall still the best method for deleting data once corrected.
You can also just
deprecate the field in the UI if you don’t need to delete it.
Apr 15, 2024, 8:48 PM
Is the v3 syntax uses cliClient or createClient constructor? I was able to make it run but, even if I am logged as admin, I get permission errors.
Apr 15, 2024, 8:58 PM
This is how you get the client for a script:
import {getCliClient} from 'sanity/cli'
const client = getCliClient()
To execute the command you’ll need to use the flag
--with-user-token
.
Apr 15, 2024, 9:14 PM
I'm getting the following error with
sanity exec scripts/batchRemoveField.js --with-user-token

  details: {
    description: 'Mutation failed: Insufficient permissions; permission "manage" required',
    items: [ [Object] ],
    type: 'mutationError'
  }
Apr 15, 2024, 9:37 PM
thank you for your time ❤️
Apr 15, 2024, 9:38 PM
Are you logged in via the CLI?
Apr 15, 2024, 9:48 PM
yes!
Apr 15, 2024, 9:49 PM
if you run
sanity debug --secrets
what role does it say you have under User?
Apr 15, 2024, 9:50 PM
User type or user role?
Apr 15, 2024, 9:51 PM
Role
Apr 15, 2024, 9:51 PM
  User role: 'administrator'
Apr 15, 2024, 9:52 PM
but user type is at normal
Apr 15, 2024, 9:52 PM
Is it possible you’re logged into a different account that doesn’t have access to this project? This can happen if you use a different provider to log in than the one you used when you created the project.
Apr 15, 2024, 9:53 PM
hmm, I only have 1 account setup. I don't use email/password to login. No specific provider
Apr 15, 2024, 9:59 PM
It could have happened accidentally if you used something like Google instead of Github to log in, for example.
Apr 15, 2024, 10:01 PM
the project was created when we were still using Google provider. We have migrated to Microsoft this year and we switched to email/password on Sanity
Apr 15, 2024, 10:03 PM
I have no problem to read but it won't allow me to manage/write. I will ask a colleague in the morning to run the script on his account. I will update this tread then. Thanks again for your time
Apr 15, 2024, 10:09 PM
Good morning rd,We have tried a few things this morning but even my manager receives the same
403 forbidden
error. I have tried to also add option in the
getCliClient
constructor like a new token created with
EDITOR
permissions and the results are the same. Is there something I did not think about to try ? Thank you.
Apr 16, 2024, 2:31 PM
What version of the CLI are you on?
Apr 16, 2024, 4:02 PM
Hi again! I was able to make it work! I did update the CLI and also modified my initial GROQ query. It seem I was also fetching items that we are unable to modify. For my current use case, the script works now. Thank you soo much for your time helping me debug and rubber duck me out of there.
Apr 16, 2024, 6:47 PM
Yay! Glad you got it figured out!
Apr 16, 2024, 6:57 PM
I suggest maybe updating the initial script. It is the first to pop up in google when you re searching for remove field in sanity. It could help more people.
Apr 16, 2024, 6:58 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.

Was this answer helpful?