I’ve just prepared some fragment of code. Maybe it would be appropriate in your context. Firstly, you should create ContentPartHandler with ListPart as a generic type and override the RemoveAsync method. Then you can get all list items and remove them (as in the example below).
public class MyListPartHandler : ContentPartHandler<ListPart>
{
private readonly IOrchardHelper _orchardHelper;
private readonly IServiceProvider _serviceProvider;
public MyListPartHandler(IOrchardHelper orchardHelper, IServiceProvider serviceProvider)
{
_orchardHelper = orchardHelper;
_serviceProvider = serviceProvider;
}
public override async Task RemovedAsync(RemoveContentContext context, ListPart instance)
{
var items = await _orchardHelper.QueryListItemsAsync(instance.ContentItem.ContentItemId);
var contentManager = _serviceProvider.GetService<IContentManager>();
var tasks = items.Select(item => contentManager.RemoveAsync(item));
await Task.WhenAll(tasks);
}
}
Finally, you should register your handler in the ConfigureServices method of the Startup class.
services.AddContentPart<ListPart>().AddHandler<MyListPartHandler>();