How would I go about making a document type not deletable? I’ve come as far as adding only once instance to the structurebuilder but it’s still possible to duplicate anddelete...

8 replies
Last updated: Feb 1, 2021

How would I go about making a document type not deletable? I’ve come as far as adding only once instance to the structurebuilder but it’s still possible to duplicate and delete it. Tried with the following: “__experimental_actions: [“update”, /* ‘create’, ‘delete’, */ “publish”],” but it didn’t do anything for it.

Feb 1, 2021, 1:29 PM

you can set up custom document actions https://www.sanity.io/docs/document-actions and filter out the DeleteAction. Something like this:

import { DeleteAction } from "part:@sanity/base/document-actions";

export default function resolveDocumentActions(props) {
  if (props.type === "something") {
    return defaultResolve(props).filter(
      (Action) => Action !== DeleteAction
    );
  }
}

Feb 1, 2021, 1:32 PM

Thanks Arjen. That did it for me, I made the following solution:

import defaultResolve from "part:@sanity/base/document-actions";
import { DeleteAction } from "part:@sanity/base/document-actions";

export default function resolveDocumentActions(props) {
  if (props.type === "siteSettings") {
    return [
      ...defaultResolve(props).filter((Action) => Action !== DeleteAction),
    ];
  }
  return [...defaultResolve(props)];
}

Feb 1, 2021, 1:39 PM

user B
Is it possible not to show that menu entirely instead of just delete action? Can’t find it in the API docs

Feb 1, 2021, 1:40 PM

return an empty array?

Feb 1, 2021, 1:41 PM

user B
I tried that but then the publish button disappears

Feb 1, 2021, 1:41 PM

only return the publish action then?

return [PublishAction]
or maybe even

return PublishAction
haven't tried it

Feb 1, 2021, 1:42 PM

Thanks I was missing the name of it

user B
couldnt find it in the docs

Feb 1, 2021, 1:59 PM

This did the trick:

return [
      ...defaultResolve(props).filter((Action) => Action === PublishAction),
    ];

Feb 1, 2021, 1:59 PM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.