Developer guides

Create a recycling bin for deleted documents via Sanity Functions

Help your editors restore deleted documents in a dataset using Sanity Functions and a singleton bin document type.

Set up a custom 'recycle bin' logic in your Studio, enabling users to restore deleted documents of a certain type with 2 clicks, using Sanity Functions and a singleton document type, to which we add some custom components using the Component API and the Sanity UI.

In this guide, you will:

  • Define a singleton document type and create your singleton document using the CLI.
    The deletedDocs.bin type will have a deletedDocLogs array with log items (objects) where we store the documentId (string), type (string), deletedAt (datetime) and documentTitle (string) of each deleted document. There can also be a more straightforward (optional) array deletedDocIds with just the _id strings.
  • Set up a Sanity Blueprint and Function which will be triggered upon deletion of a subset of documents. The document handler will then patch the deleted document to the logs of the bin singleton document.
  • Create a custom item component for the log items, including the intent button for opening the deleted documents in question.
  • Create a second function, which will remove all document logs, which have been restored already.

In this guide, we will use TypeScript to make the code more reliable, but you can use JavaScript if you prefer. If you don't know how to do this, you can ask in our Discord community for help!

You can find the whole code for the solution with functions here and the older version with webhooks here.

Background: Restoring deleted Documents using the _id

When you delete a document, you can restore it using the unique document _id (either via the History API, or the Studio). In the Studio it is as simple as opening up the document in the structure using the default folder for that particular document type and adding the ID to the URL:

https://<domain>/studio/default/structure/<document type name>;<deleted document _id>

Although this trick is helpful, you would still need the know the deleted document _id.

Intent routing in the Studio

Internally, Sanity typically uses an Intent Link to navigate to a document in the structure. We can use the same intent to open deleted documents and use the restore functionality automatically proposed for any deleted document opened in the Studio.

Workflow

Flowchart demonstrating document deletion, logging in a recycling bin, restoration, and subsequent cleanup from the bin logs.
This is how the deletion of a document will then trigger a function, which adds a log item of the deleted document to the bin singleton document. The log will then enable you to open the deleted document again and restore it. Once you restore a document the second function will remove the log entry.

Step 1: Singleton document schema

Create a document type called deletedDocs.bin in your schema folder (in our case we have an additional subfolder called singletons) and add it to your schema as a singleton:

We set all arrays to readOnly and also hide away the title field since we will set the title in the next step and only need it for a better UI.

In addition we disabled sorting for arrays to have a cleaner look.

Custom TypeScript interface for the deletedDocLogs items

As we always want to make the TypeScript Dogs happy, we need to extend the Sanity ObjectItem with our data keys.

Add the custom interface to the schema definition or a separate types file.

// import ObjectItem from sanity

export interface LogItem extends ObjectItem {
  docId: string
  deletedAt: string
  type: string
  documentTitle: string | 'Unknown 🥲'
  deletedBy?: string
  revisionId: string
}

Create a singleton document via the CLI

For the next step, we will create a private document by using a dot in _id. Our personal choice was to use the same logic in the _type name, but you can use a name without a dot if you want to.

At the root of your project, create a newBinSingleton.json and add this data to it:

{
  "_id": "deletedDocs.bin",
  "_type": "deletedDocs.bin",
  // feel free to add your own title 
  "title": "Bin: Deleted Document Logs"
}

Next, you need to open your terminal in the root of the project folder and create a document via the CLI:

$ sanity documents create newBinSingleton.json

// or if you dont have @sanity/cli installed globally
$ npx sanity documents create newBinSingleton.json

🥳 Now, there should be a singleton document visible in your structure.

Bin singleton document list item in structure
Bin singleton document in structure

Step 2: Adding custom components

Now we are ready to give our arrays some bling and add custom input components 💅.

We will:

  • Remove the Add Item buttons from the arrays (go to section).
  • Add custom components:
    • DeletedDocIdInputComponent.tsx: input component for deletedDocIds array (go to section).
    • DeletionLogItemComponent.tsx: item components for log objects in the deletedDocLogs array (go to section).

Remove the Add Item buttons from arrays

Because we don't need a UI for adding new items to any of our arrays, we will not only set them to readOnly: true, but also remove the buttons under the inputs by adding custom input components.

In those components, we define that we want to render out the default inputs (by using props.renderDefault from the Component API) minus the arrayFunctions (which will render out the button to add new items to arrays).

Your bin document should look like this now:

Screenshot of the bin document and their arrays without the default add item button.
By setting the arrayFunctions to null the buttons for adding items are removed from the array inputs.

Custom input component for the simple (optional) deletedDocIds array items

Add a file DeletedDocIdInputComponent.tsx for the simple string items and add the component to your deletedDocIds array member string field.

Then add the custom input component to the deletedDocIds array by adding this snippet to the deletedDocId array member:

Your field should look like this now

screenshot of how the array will look now (with dummy data in this case)
This is how the array will look now (with dummy data in this screenshot)

Custom item component for the log objects (deletedDocLogs array members)

Now that we have the easy part behind us, we can dive deeper into the restoring functionality itself.

First, we need to create a file DeletionLogItemComponent.tsx and override the default preview since we do not want to use the array for editing the log object values, but only display each deletion and add a button which will lead us to the deleted document in the structure, where we can restore it.

With this item component in our pockets, we still have to add it to our log object array members:

// Add this to your `log` object, in your `deletedDocLogs` array
components: {
  item: DeletionLogItemComponent,
},

Very good, our document should look like this now 💅: Super fancy and easy to use!

Screenshot of document with some deleted document logs
This is how it will look in when we have some deleted document logs

Step 3: Setting up your Blueprint and Functions

Functions and Blueprints are a new feature for all Sanity projects which allow you to create infrastructure as code—which means you can automate workflows via code directly and deploy those to the Sanity infrastructure. No more need for custom API endpoints or other external server functions.

If you haven’t tried them before, please make sure to read the docs and have a look at our 101 YouTube video and especially the Sanity Learn module.

Use CLI to create both a blueprint and functions

You can follow the Sanity learn module to initialize and add a blueprint and function via the CLI or follow the Functons quick start.

You will need 2 functions:

  • recyclingBin adds logs for every deleted document.
  • cleanUpBinLogs removes the recreated documents from the logs.

Initialize each function, add them to your blueprint, and you should now have these files / folders in your project root:

While you’re in the CLI, now is a good time to install two dependencies you’ll need for the functions. They’ll both use the Sanity client and the id-utils helper library.

Document handler and blueprint resource for recyclingBin function

Now that you’ve created the overall infrastructure, let’s start setting up our resource and document handlers.

While the CLI gives you a resource for your blueprint for each function we need to modify it to fit our needs.

High traffic datasets & drafts

Let’s have a closer look at the filter and projection.

The filter needs to be as narrow as possible for any function or webhook. In this case we only want to trigger it for those document types that should be restorable, in my case thats language, listOption and page, but you have to add yours.

The projection defines, which values should be passed down from the trigger-document to the document handler of your function. This is particularly important for all deleted documents, since you will not be able to directly query them anymore. In our case we need the document and revision ID, the time of deletion and the identity of the person/token who deleted the document (returns the ID), and we also need something human readable. In my case I want to have a title, but since the document types use both title and name I coalesce them. You can also adjust which field values you want to use here.
If you need to include values from other documents you can query them in the document handler.

DocumentHandler and resource for cleanUpBinLogs function

Similar to the other function we need to define the blueprint resource and document handler for the cleanup workflow.

High traffic datasets & drafts

Since we only have to check if the newly created document or draft is actually a restored one, the _id is enough in the projection.

Test and deploy your blueprint

Now that you have everything in order you can test your functions using the dev console or the CLI (see docs).

If you have been satisfied that your functions work well and don’t cause a loop, you change all commit options in the document handlers to dryRun: false and deploy them to our infrastructure using the CLI.

Finished!

Now you are ready to test things in your project.

Don't forget to have a look at my Meetup Repo (specifically the recycling bin branch) where you can find a full version of the code.

Was this page helpful?