Mediafiles are deleted before MediaService.Saving has been completed

Umbraco 13.9.3

I’m trying to implement logic to block user from editing specific media files, but it seems I can’t stop user from deleting file itself? Can I achieve this somehow?

I also tried it in a clean setup using notifications:

public class MediaSaving() : INotificationHandler<MediaSavingNotification>
{
    public void Handle(MediaSavingNotification notification)
    {
        foreach (var node in notification.SavedEntities)
        {
            node.ResetDirtyProperties();

            notification.CancelOperation(new EventMessage("cancel", "cancel", EventMessageType.Error));
        }
    }
}

I believe your code handles MediaSavingNotification, which only intercepts save/edit operations. To block deletion, you could try to handle MediaDeletingNotification. So changing this line and testing with may put you on track:

public class MediaSaving() : INotificationHandler<MediaDeletingNotification>

I don’t think MediaDeletingNotification is the right choice for this use case.

IIRC MediaDeletingNotification will only trigger when the Recycle Bin is being cleared or the item is being marked for deletion in the bin.

MediaMovingToRecycleBinNotification might be a better choice to stop the action before it’s moved to the bin.

The full list of notification can be found here if OP needs it:

Hi, thank you for your replies. Sadly no notification handles file deletion. So I went different route - I set file upload property to readonly using “SendingMediaNotification“ so that “Remove files“ option is removed. Only downside was that file upload property by default didn’t pass down readonly value, so I created copy of file upload file property which does this and it works great.