Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Stop images from being deleted when they are being used in required fields

default discord avatar
adam2204last year
4

this is a fairly large post but I feel like these things are related in a big way



The issue

I noticed that I am able to delete files that are being used in required fields on published pages and the page stays published.


I don't know if this can be called an issue but I can see how this could cause issues for people working on large sites with lots of content and deleting something that is being used or is required could cause some problems and could be hard to find the issue.



Feature request

It would be nice to get a warning that the item that is being deleted is in use on some page or post, And if the item is being used in a required field it should throw and error saying it cant be deleted unless it is removed from the field.


Similarly it would be nice to see where images and files are being used when viewing them in the media collection so it is easier to find them in large content collections



Im using version beta-94


I have also tested this on a fresh install of demo-website in payload 2.0 latest and i am still able to delete required items



Stop images from being deleted when they are being used in required fields

  • default discord avatar
    notchrlast year

    This would be a nice feature request to make



    I would make the request here:

    https://github.com/payloadcms/payload/issues/new/choose

    I'm not sure if the

    beforeDelete

    hook on collections has the ability to prevent the deletion, but if that's possible, then it would be a great way to check the relations prior.



    https://payloadcms.com/docs/hooks/collections#beforedelete
  • default discord avatar
    steadysnaillast year

    It would be nice if this we're in-built, but In the meantime you can make a beforeDelete hook to check the collections where you have a relationship to the current collection. I'm doing this on several collections in our customer management app. For example we have customer, account, and license collections. An account record can not be deleted if there are any related licenses and a customer record can not be deleted if there are any related accounts. The downside to this approach is you'd have to update the beforeDelete hook anytime a relationship is added to a new collection.

  • default discord avatar
    adam2204last year

    ok thanks for the info, i will definitely try using the beforeDelete hook to manage the images. But as you said, when adding new collections you need to manually update the hook.


    I think it would be great to see this implemented as a default function specially for the media collection since it is not a normal collection and is used in most projects.



    https://github.com/payloadcms/payload/issues/7982
  • default discord avatar
    nooflorist11 months ago

    I have implemented this leveraging the searchPlugin to index any relationships of any kinds of documents. Then a

    beforeDelete

    hook looks for documents having relationships to the to-be-deleted document in the search index, as well as all globals.



    This provides a complete deletion protection usable for all kinds of collections.



    I have detailed a solution here:


    https://github.com/payloadcms/payload/discussions/558#discussioncomment-13656269
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.