Is it possible to delete a record with all his relations (cascade delete) without using hooks to delete each related record?
Hey @wojciechkrol — yes, this is possible! You'll need to write some hooks to do it. You could write a hook that can be used to traverse your collection schema and find any
relationship fields, and for each one, you'd read the value of the relationship field and then delete all related documents using Payload's local API.
Know what I mean?
Sorry, I didn't see that you specified
without hooks. Honestly though I think that's probably the best way to do this. It could even be wrapped up as a neat little plugin.
We don't have any use for this at the moment, but if one of our own projects needs it, we'll certainly build it into a plugin and make it public. If you do so, please share it! It shouldn't be too hard to build.
Thank you for your answer. I will try to make a universal hook for this and let you know if I was successful :)
This would be extremely helpful to have as an option to enable on a per-collection basis where a collection would then be able check for any relationship type fields referencing itself when deleting an item.
While I do understand a hook could resolve this having a built-in solution should, in my opinion, be of a very high priority since it would prevent end-users from breaking stuff (e.g. broken media, broken links etc).
I started working on this then I got covid and my wife had a baby.
If somebody wants to fork off my PR, this is up for grabs. Otherwise I'll finish it after finishing the admin UI translations.