Hello everyone,
I'm creating a scheduler to clean a folder where I have to delete assets that are more than X days old.
The logic is similar to when the expiration day of the asset is reached, but I've to add delete the asset from the folder in author.
-Unpublish
-Unpublish in Dynamic Media.
-Unpublish from Brand.
-Delete asset
My question is, what is the best way to delete an asset in AEMasaCS?
i have something similar to:
Session session = resourceResolver.adaptTo(Session.class);
//unpublish assets from Brand Portal
this.damSyncService.unpublishResourcesFromMP(assetPaths, resourceResolver);
for (String assetPath : assetPaths) {
ReplicationStatus assetReplicationStatus = replicator.getReplicationStatus(session, assetPath);
if (assetReplicationStatus.isActivated()) {
replicator.replicate(session, ReplicationActionType.DEACTIVATE, assetPath);
resourceResolver.getResource(assetPath).adaptTo(Node.class).remove();
}
}
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi, here it's my solution
private void deleteFromScene7(Resource assetResource, ResourceResolver resourceResolver) {
try {
S7Config s7Config = s7ConfigResolver.getDefaultS7Config(resourceResolver);
String scene7ID = this.s7Service.getScene7ID(assetResource);
if (scene7ID == null || s7Config == null) return;
if (assetResource.isResourceType(NT_SLING_FOLDER)) {
s7Service.deleteFolder(scene7ID, s7Config);
} else if (assetResource.isResourceType(NT_DAM_ASSET)) {
s7Service.deleteAsset(scene7ID, s7Config);
}
} catch (Exception le) {
logger.error("Error trying to delete asset from Scene7", le);
}
}
Hi @VictorToledo,
If in your code you have Session object I would go with below code:
session.removeItem(assetPath);
session.save();
Why? Because you need only path to asset you would like to remove. You do not have to fetch any object, and do any other unnecessary operations. Of course using JCR api is probably not the most elegant but in this case it will be most effective.
Alternatively you can consider to use Sling api, but this will cost you one additional operation - to retrieve/create Resource object. In my opinion, because you do not want to ready anything specific from resource object this is a bit redundant operation. But of course using Sling api, is always preferable instead of using JCR api which is low level api.
So the same operation using Sling api will be like that:
Resource res = resourceResolver.getResource(assetPath);
resourceResolver.delete(res);
resourceResolver.commit();
The way you did it so far, looks to be the less effective way. First of all you are mixing 2 apis in one call starting with Sling, and running final operation from JCR api. But more important, is that you are doing unnecessary operation, creating Resource and Node objects.
Hello @VictorToledo
The session APIs should not create problem in AEMaaCS.
Can I possibly request you to check one use-case?
- Block your replication queue.
- Deactivate the asset.
At this point, the asset is stuck in queue. Thus, if one forcibly clears the queue. The asset will continue to exist on publish, DM and Brand-portal.
It might not be wise at this point to delete the asset on author.
I would suggest you to split the operation into 3:
- Unpublish the assets
- Check the replication queues status.
- If replication queue looks good, delete the asset.
My recommendation would be to create a sling job and execute the workflow from there to do following things.
do you know how I can programmatically unpublish on dynamic media?
Why do you want to unpublished it programmatically? Why not using ootb configured workflow to be triggered ?
because there is not a ootb workflow for this on AEMasaCS
Selective unpublished should work. I don’t have dynamic media configured on my local to check the workflow but you can reference this https://docs.mktossl.com/docs/experience-manager-cloud-service/content/assets/dynamicmedia/selective...
Hi, here it's my solution
private void deleteFromScene7(Resource assetResource, ResourceResolver resourceResolver) {
try {
S7Config s7Config = s7ConfigResolver.getDefaultS7Config(resourceResolver);
String scene7ID = this.s7Service.getScene7ID(assetResource);
if (scene7ID == null || s7Config == null) return;
if (assetResource.isResourceType(NT_SLING_FOLDER)) {
s7Service.deleteFolder(scene7ID, s7Config);
} else if (assetResource.isResourceType(NT_DAM_ASSET)) {
s7Service.deleteAsset(scene7ID, s7Config);
}
} catch (Exception le) {
logger.error("Error trying to delete asset from Scene7", le);
}
}
one more point to consider , that there is no active reference of the asset on the site author pages.
Thanks
Views
Replies
Total Likes