I have not been able to find a perfect solution but you could try this.
The call stack is different if the call is done by a folder or file delete:
With this info we can add this validation:
public class ValidateFileNotDeletedWithFolder : ValidationRuleBase<File>
public override ValidationResult Validate(File entity, ValidationMode validationMode)
var result = new ValidationResult();
if (validationMode != ValidationMode.Remove)
var isSingleFileDelete = Environment.StackTrace.Contains("WebApi.Media.Controllers.FileController.Delete");
result.AddError("*", "Files cannot be deleted together with folder.");
It verifies that files can only be deleted if the call comes through
FileController.Delete() - depending on where you want to allow delete from you will need to add other exceptions here also.
When testing I also noted another issue that validation errors for files does not show when they are triggered from a folder-delete, instead a general permission-message is shown:
Bug report is created for this: https://docs.litium.com/support/bugs/bug_details?id=50807