Draft document auto-restores after deletion in Sanity Studio

5 replies
Last updated: May 31, 2022
Hello
TLDR; When deleting a non-published draft in Studio, it sometimes gets restored as an untitled document.

We have a pretty standard setup where we display a list of article documents in the Studio.

We have noticed that if you create a new article, then delete the draft without publishing it, it will then be restored without any content. The restored version does have the same id as the original draft document. Now, if you then fill in all fields, then publish the “auto restored” document, then publish and delete it, it will once again be “restored” as an empty draft.

Using
sanity documents delete ...
from the command line does not cause this restoring behavior.
This happens consistently with one of our document types, whilst another almost identical document does not have this issue.

Has anybody encountered this, tips?
AI Update

This is a tricky issue, and the fact that it happens consistently with one document type but not another is the key diagnostic clue. Since CLI deletion works fine, this points to something in your Studio configuration rather than a Content Lake issue.

Here are the most likely causes and how to debug them:

Compare the Two Document Types Side-by-Side

Since you have one document type that works correctly and one that doesn't, do a detailed diff of their schema definitions. Look specifically at:

  • Reference fields: Fields that reference other documents, especially if they have custom preview components
  • Custom input components: Any field using a custom React component
  • Validation rules: Complex validation that might maintain document state
  • Preview configurations: Custom preview selectors or components
  • Field-level configurations: Hidden fields, read-only fields, or conditional field logic

Check Your Structure/Desk Configuration

If you have a custom structure configuration (in sanity.config.ts or a separate structure file), look for anything specific to the problematic document type:

  • Custom list views or filters
  • Document list preview configurations
  • Any structure items that specifically query or reference that document type

Inspect Network Traffic During Deletion

This is your best diagnostic tool. Open browser DevTools → Network tab, then delete the draft. Watch for:

  1. The DELETE mutation (should succeed)
  2. Any subsequent CREATE or PATCH mutations that happen immediately after
  3. The payload of those follow-up mutations will tell you exactly what's recreating the document

Filter the network requests to just show calls to api.sanity.io to reduce noise.

Check for Document-Level Subscriptions

Look through your codebase for anything that might be subscribing to or maintaining a reference to documents of that type:

  • Custom Studio plugins
  • Form components that use useFormValue or useDocumentStore
  • Any real-time listeners or observers

Known Studio Quirks

There have been reports of Studio's presence system (which tracks who's editing what) sometimes causing unexpected behavior with drafts, though this is rare. The fact that the restored document has the same ID as the original is expected (drafts use drafts.{id} as their document ID), but the restoration itself is not normal behavior.

  1. Isolate the difference: Create a minimal version of the working document type, then gradually add fields/config from the problematic type until you reproduce the issue
  2. Check Studio version: Make sure you're on the latest Studio v3 (or v4) version, as some draft-handling bugs have been fixed in recent releases
  3. Report with reproduction: If you can create a minimal reproduction case, report this to Sanity support - this could be a Studio bug that needs fixing

The network inspection is really your best bet here - it will show you exactly what operation is recreating the document and that will point you to the source of the problem in your configuration.

Have you noticed if the "restored" document appears immediately after deletion, or does it take a few seconds? That timing might also provide clues about whether it's a synchronous operation in your Studio code vs. something async.

Show original thread
5 replies
Hi Jørn. This behaviour permits you to restore the document while you still have the
_id
(via the URL). It's actually possible to do the same thing (restore a deleted document) outside the studio using the History API —you'd just need the document
_id
.
I might have explained this a bit poorly, or I’m perhaps misunderstanding your answer.
The issue is that for every document deleted by an editor inside of Sanity Studio a new document (with the same id as the deleted one) will appear in Studio, but the document will have no content what so ever.
This means that, in practice, you can’t get rid of any documents, you will instead end up with a list of documents with no content.

Are you saying that this is a setting or something, since it’s only happening for some document types for us?
I think I figured it out.A custom component was triggering an edit to the removed document (setting a default value), this is probably why the document was automatically re-created.
Fixing the custom component should probably do the trick.
Normally, that document (with the
_id
you just deleted) will “remain” until you navigate away from it. It’s then gone unless you restore it with the History API (and you need to know the
_id
for that). It sounds like you have figured it out, though, as making any change to a document that has been deleted will cause it to be re-created with that change. Please let us know how it goes!
Updating the custom component to avoid triggering a PatchEvent when a document is deleted fixed the issue.
Thank you for the input, really helpful
👍

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?