Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

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

Avatar

Level 3

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
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 3

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

 

View solution in original post

10 Replies

Avatar

Community Advisor

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.

 

Avatar

Level 3

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?

Avatar

Community Advisor

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

Avatar

Community Advisor

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

 

Avatar

Level 3

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

Avatar

Community Advisor

Why do you want to unpublished it programmatically? Why not using ootb configured workflow to be triggered ?

Avatar

Community Advisor

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...

Avatar

Correct answer by
Level 3

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

 

Avatar

Level 4

@VictorToledo ,

 

one more point to consider , that there is no active reference of the asset on the site author pages. 

 

Thanks