Cannot delete locked image - document type no longer exists in schema

8 replies
Last updated: Aug 2, 2022
Hey everyone! I am updating an old studio for a client of mine and I need to delete some old images (using the media plugin).. However the sanity editor is not letting me delete the image because it is locked to an old document (document was named
editorialPage
which we no longer use). I went into my terminal and deleted the document manually using
sanity documents delete editoralPage
, but it says the document is not found.. Very odd. When I check the image reference it still says
A document of the unknown type editorialPage
... Any help with this? Ideally it would just be great to delete that document altogether
AI Update

It sounds like you're dealing with a document that's stuck in your dataset with an unknown/deleted document type! This is a common issue when you've removed a schema type from your Studio configuration but documents of that type still exist in your dataset.

The problem with sanity documents delete editorialPage is that the CLI command expects a document ID, not a document type name. Since the error says "A document of the unknown type editorialPage", the document likely has a different _id value (like editorialPage-123 or something similar).

Here are the proper ways to solve this:

Option 1: Find and Delete Using Vision Plugin

Go to your Sanity Studio and open the Vision plugin (the eye icon in the toolbar). Run this GROQ query:

*[_type == "editorialPage"]

This will show you all documents with that type, including their actual _id values. Note the IDs, then you can delete them using the CLI with the correct ID:

sanity documents delete "THE-ACTUAL-ID-HERE"

Remember to check for both published and draft versions - draft IDs have a drafts. prefix:

sanity documents delete "editorialPage-123"
sanity documents delete "drafts.editorialPage-123"

Option 2: Delete Programmatically Using Sanity Client

If you have multiple documents to delete, you can use the Sanity Client in a Node.js script:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  token: 'your-token-with-write-access',
  apiVersion: '2024-01-01',
  useCdn: false
})

// Query for all editorialPage documents
const docs = await client.fetch('*[_type == "editorialPage"]')

// Delete each one
for (const doc of docs) {
  await client.delete(doc._id)
  console.log(`Deleted ${doc._id}`)
}

Option 3: Use the HTTP Mutation API

You can also use the Mutation API directly with curl. First find the document IDs, then delete them:

curl -X POST \
  https://YOUR-PROJECT-ID.api.sanity.io/v2024-01-01/data/mutate/YOUR-DATASET \
  -H "Authorization: Bearer YOUR-TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "mutations": [
      {"delete": {"id": "THE-ACTUAL-DOCUMENT-ID"}}
    ]
  }'

About the Media Plugin Lock

Once you properly delete the document (and any draft versions), the media plugin should automatically unlock the image since there won't be any references pointing to it anymore. If it still shows as locked after deletion, try refreshing your Studio or checking if there are other documents referencing that image.

The Vision plugin approach is usually the quickest for finding these orphaned documents with unknown types. Let me know if you need help with any of these methods!

Show original thread
8 replies
Hello
user S
In order to bulk detele, you will need to use this functionality:
https://www.sanity.io/guides/bulk-deletion-using-sanity-cli Or you could use one of those snippets:
https://www.sanity.io/answers/how-to-delete-all-docs-of-a-specific-doc-type-p1605106695431800 Tell me if you were able to delete
editorialPage
with this approach πŸ™‚
user S
is
editorialPage
the
_id
of the document or the title?
sanity documents delete
expects to receive an
_id
of a document so that may be your issue!
If you don't know the
_id
you can first run
sanity documents query '*[title == 'editorialPage']'
. That should print the
_id
for you to use when you run
sanity documents delete
.
Hey
user M
I just tried this.. Seems like it might be the issue. I get the following response back:
--api-version not specified, using `v1
Not sure what this is for..
Can you try adding in an API version like this:
sanity documents query --api-version v2021-06-07 '*[title == "editorialPage"]'
Hmm I just tried that and a blank
[]
is the return object lol. Let me attach a photo of whats going on.. BTW this is a major issue, but it would be great to have old, unused photos deleted.
Oh, that does have a document _id then! If that document is no longer in use, you can just pass that _id into your
sanity documents delete
command.
Haha it worked! πŸŽ‰ Thanks
user M
your're a wizard!
yer a wizard

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?