this is a fairly large post but I feel like these things are related in a big way
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.
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
This would be a nice feature request to make
I would make the request here:
https://github.com/payloadcms/payload/issues/new/chooseI'm not sure if the
beforeDeletehook 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.
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.
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.
I have implemented this leveraging the searchPlugin to index any relationships of any kinds of documents. Then a
beforeDeletehook 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:
Star
Discord
online
Get dedicated engineering support directly from the Payload team.