When deleting a CommerceMedia programmatically from a Product/Variation, after the deletion, it leaves the asset row intact like the below:
The problem is that Commerce API is used incorrectly. Instead of using writableContent object, writableCommerceMedia object is used which means the reference between variation and media still remains.
var writableContent = content.CreateWritableClone<EntryContentBase>(); CommerceMedia assetReferenceToRemove = writableCommerceMedia.FirstOrDefault(x => x.AssetLink.Equals(mediaData.ContentLink)); writableCommerceMedia.Remove(assetReferenceToRemove); <== here is the problem |
Use the API below will solve the problem:
var writableContent = content.CreateWritableClone<EntryContentBase>(); CommerceMedia assetReferenceToRemove = writableContent.CommerceMediaCollection.FirstOrDefault(x => x.AssetLink.Equals(mediaData.ContentLink)); writableContent.CommerceMediaCollection.Remove(assetReferenceToRemove); _contentRepository.Save(writableContent, SaveAction.Publish, AccessLevel.NoAccess); |