Best way to delete assets programatically in AEM as a Cloud Service
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();
}
}