Stop Media from Been Deleted / Moved to Recycle Bin - Umbraco 13

Hi,

Is there any way to stop a user from deleting a folder or file, We have a number of critical folders in the media folder, yesterday one one of the members of staff deleted one of the critical folders, that almost killed the site. Is there a way to stop Specific Folders or Files from been deleted.

I have been looking at Notification Handlers but can’t stop the move to recycle bin event.

Hi @darrenhunterEmerald

There’s a MediaMovingToRecycleBinNotification handler, you should be able to stop the move based on whatever condition you need if you can somehow identify important folders or files…

Justin

I have come up with the following code:

using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
namespace UmbracoCMS.App_Code.MediaNotice
{
public class MediaDeleteNotificationHandler : INotificationHandler
{

    private readonly ILogger<MediaDeleteNotificationHandler> _logger;

    public MediaDeleteNotificationHandler(ILogger<MediaDeleteNotificationHandler> logger)
    {
        _logger = logger;
    }

    public void Handle(MediaMovedToRecycleBinNotification notification)
    {

       
        if (notification.MoveInfoCollection.Any())
        {

            foreach(var item in notification.MoveInfoCollection)
            {
                var i = item;

                _logger.LogInformation($"Moving to Recycle Bin {i.Entity.Name}");


                if (i.Entity.Id== 1195)
                {
                    _logger.LogError($"Moving to Recycle Bin {i.Entity.Name} ** This should not be deleted **");


                    notification.Messages.Add(new EventMessage(
                      "Notification",
                      "This Media Item should not be deleted or moved to the Trash folder, this will cause major site issues.",
                      EventMessageType.Error));

                  
                    
                }

               
            }


        }

But there no cancel option I can find. 
1 Like

You need the MediaMovingToRecycleBinNotification, which happens first and you should be able to cancel - not the MediaMovedToRecycleBinNotification as that is too late and happens afterwards.

thank you for your help!

1 Like