Best way to delete assets programatically in AEM as a Cloud Service | Community
Skip to main content
Level 2
November 17, 2023
Solved

Best way to delete assets programatically in AEM as a Cloud Service

  • November 17, 2023
  • 2 replies
  • 4560 views

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(); } }
 
i'm using node.remove() as the previous aem versions
 
thanks in advance
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by VictorToledo

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-publishing.html?lang=en#selective-unpublish-manage-publication


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); } }

 

2 replies

lukasz-m
Community Advisor
Community Advisor
November 17, 2023

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.

 

Level 2
November 17, 2023

Thanks @lukasz-m , you're right to use session.removeItem, i forgot it.

What do you think with cloud service? can i have some problem?

aanchal-sikka
Community Advisor
Community Advisor
November 18, 2023

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.

Aanchal Sikka
DPrakashRaj
Community Advisor
Community Advisor
November 19, 2023

My recommendation would be to create a sling job and execute the workflow from there to do following things.

  • check out the assets(similar to locking pages) so that no one can do any edit or publish the asset
  • Unpublish
  • Unpublish in Dynamic Media.
  • Unpublish from Brand.
  • Check in again might not required if done with same user
  • delete it

 

Level 2
November 23, 2023

do you know how I can programmatically unpublish on dynamic media?

VictorToledoAuthorAccepted solution
Level 2
November 24, 2023

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-publishing.html?lang=en#selective-unpublish-manage-publication


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); } }