✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Issue with deleting non-published drafts in Studio causing them to be restored as empty documents.

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?
May 31, 2022, 3:28 PM
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
.
May 31, 2022, 5:08 PM
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?
May 31, 2022, 5:50 PM
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.
May 31, 2022, 6:16 PM
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!
May 31, 2022, 6:24 PM
Updating the custom component to avoid triggering a PatchEvent when a document is deleted fixed the issue.
Thank you for the input, really helpful
👍
May 31, 2022, 6:29 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?