We need to implement functionality where it removes unused asset from the catalog items and then delete an asset permanently. Below is my code, where it goes through each catalog items, and try to remove association when particular asset type is no longer being used. When I execute this code, It's not removing linking between a catalog items and an asset. What could be the cause of this? I have restarted the site and cleared the browser cache, still its not working.
var contentBase = catalogContent.CreateWritableClone<EntryContentBase>(); var commerceMediaCollection = contentBase.CommerceMediaCollection.CreateWritableClone();
foreach (CommerceMedia commerceMedia in commerceMediaCollection) { IInRiverAsset resource; bool success = this._contentLoader.TryGet<IInRiverAsset>(commerceMedia.AssetLink, out resource); if (success && resource.ResourceAssetType != null && !Enum.TryParse(resource.ResourceAssetType, out assetType)) { contentBase.CommerceMediaCollection.Remove(commerceMedia); } }
I have applied another route where I have created SQL script, and able to delete asset linking and related data direct from CMS and Commerce SQL databases, and unused asset is not visible in the catalog. I would like to avoid database route. Is there any way to remove asset linking and delete them permanently?
We need to implement functionality where it removes unused asset from the catalog items and then delete an asset permanently. Below is my code, where it goes through each catalog items, and try to remove association when particular asset type is no longer being used. When I execute this code, It's not removing linking between a catalog items and an asset. What could be the cause of this? I have restarted the site and cleared the browser cache, still its not working.
var contentBase = catalogContent.CreateWritableClone<EntryContentBase>();
var commerceMediaCollection = contentBase.CommerceMediaCollection.CreateWritableClone();
foreach (CommerceMedia commerceMedia in commerceMediaCollection)
{
IInRiverAsset resource;
bool success = this._contentLoader.TryGet<IInRiverAsset>(commerceMedia.AssetLink, out resource);
if (success && resource.ResourceAssetType != null && !Enum.TryParse(resource.ResourceAssetType, out assetType))
{
contentBase.CommerceMediaCollection.Remove(commerceMedia);
}
}
this._contentRepository.Save(contentBase, SaveAction.Publish, AccessLevel.NoAccess);
I have applied another route where I have created SQL script, and able to delete asset linking and related data direct from CMS and Commerce SQL databases, and unused asset is not visible in the catalog. I would like to avoid database route. Is there any way to remove asset linking and delete them permanently?
Versions:
Commerce: 13.24.0
CMS: 11.19.0