public async Task <Either <ActionResult, Unit> > Delete(Guid releaseId, IEnumerable <Guid> ids, bool forceDelete = false) { return(await _persistenceHelper .CheckEntityExists <Release>(releaseId) .OnSuccess(async release => await _userService.CheckCanUpdateRelease(release, ignoreCheck: forceDelete)) .OnSuccess(async release => await ids.Select(id => _releaseFileRepository.CheckFileExists(releaseId, id, FileType.Data)) .OnSuccessAll()) .OnSuccessVoid(async files => { foreach (var file in files) { var metaFile = await GetAssociatedMetaFile(releaseId, file); if (await _releaseFileRepository.FileIsLinkedToOtherReleases(releaseId, file.Id)) { await _releaseFileRepository.Delete(releaseId, file.Id); await _releaseFileRepository.Delete(releaseId, metaFile.Id); } else { await _dataImportService.DeleteImport(file.Id); await _blobStorageService.DeleteBlob( PrivateReleaseFiles, file.Path() ); await _blobStorageService.DeleteBlob( PrivateReleaseFiles, metaFile.Path() ); // If this is a replacement then unlink it from the original if (file.ReplacingId.HasValue) { var originalFile = await _fileRepository.Get(file.ReplacingId.Value); originalFile.ReplacedById = null; _contentDbContext.Update(originalFile); await _contentDbContext.SaveChangesAsync(); } await _releaseFileRepository.Delete(releaseId, file.Id); await _releaseFileRepository.Delete(releaseId, metaFile.Id); await _fileRepository.Delete(file.Id); await _fileRepository.Delete(metaFile.Id); if (file.SourceId.HasValue) { var zipFile = await _fileRepository.Get(file.SourceId.Value); await _blobStorageService.DeleteBlob( PrivateReleaseFiles, zipFile.Path() ); // N.B. No ReleaseFiles row for source links await _fileRepository.Delete(zipFile.Id); } } await DeleteBatchFiles(file); } })); }