Deleting Commerce Connect Media leaves the asset row intact

  • Updated

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 Connect 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>();
var writableCommerceMedia = writableContent.CommerceMediaCollection.CreateWritableClone();

CommerceMedia assetReferenceToRemove = writableCommerceMedia.FirstOrDefault(x => x.AssetLink.Equals(mediaData.ContentLink));

writableCommerceMedia.Remove(assetReferenceToRemove); <== here is the problem

_contentRepository.Save(writableContent, SaveAction.Publish, AccessLevel.NoAccess);

 

 

Use the API below will solve the problem: 

var writableContent = content.CreateWritableClone<EntryContentBase>();
var writableCommerceMedia = writableContent.CommerceMediaCollection.CreateWritableClone();

CommerceMedia assetReferenceToRemove = writableContent.CommerceMediaCollection.FirstOrDefault(x => x.AssetLink.Equals(mediaData.ContentLink));

writableContent.CommerceMediaCollection.Remove(assetReferenceToRemove);

_contentRepository.Save(writableContent, SaveAction.Publish, AccessLevel.NoAccess);