Expand my Community achievements bar.

Get ready! An upgraded Experience League Community experience is coming in January.

Anyway to get Asset resource on pre Delete event in AEM cloud

Avatar

Level 2

I need to read the asset metadata and send the data to 3rd party API in back end java, when the author click on "Delete" on Dam assets. 

Currently in AEM cloud, we are not supposed to customize the OOTB "Delete" functionality(per client), tired with custom replication event listener, launcher with custom workflow step but by that time these triggered asset resource is already null.

Any way to read the asset resource on pre "Delete" event.

 

Appreciate your help. Thanks;

2 Replies

Avatar

Community Advisor

Hi @Srikanth23696837zrx0 ,

 

There are 3 ways how to solve listen asset removal events:

1) EventHandler (JCR / Sling Events)

You can listen to repository or Sling events (e.g. asset removed events). 

I checked in the /system/console/events and figured out that this kind of event happened before other workflow launchers and listeners.

 

@Component(
        service = EventHandler.class,
        property = {
                EventConstants.EVENT_TOPIC + "=com/day/cq/dam"
        }
)
public class AssetRemovedEventHandler implements EventHandler {
    @Override
    public void handleEvent(final Event event) {
        String type = String.valueOf(event.getProperty("type"));
        if ("ASSET_REMOVED".equalsIgnoreCase(type)) {
            String path = String.valueOf(event.getProperty("path"));
            // Implement your logic here for handling the removed asset
        }
    }
}

 


2) ResourceChangeListener (Event-driven, but Post-Delete)

ResourceChangeListener can observe DAM delete events:

@Component(
        service = ResourceChangeListener.class,
        property = {
                "resource.change.types=REMOVED",
                "resource.paths=/content/dam"
        }
)
public class AssetRemovedResourceChangeListener implements ResourceChangeListener {
    @Override
    public void onChange(@NotNull List<ResourceChange> changes) {
        for (ResourceChange change : changes) {
            if (change.getType() == ResourceChange.ChangeType.REMOVED) {
                String removedPath = change.getPath();
                // Implement your logic here for handling the removed asset
            }
        }
    }
}

3) Workflow Launcher

  • Create a workflow launcher:
  • Event Type: Removed
  • Nodetype: dam:Asset
  • Path: define a path
  • Workflow Model: your new workflow model

My recommendation:

Use the approach #1 with event handler, start sling job and capture a metadata before it's deleted. You can try to capture an asset metadata before start of sling job, but it has to be a very quick action, because event handlers can be banned if they take more than few seconds (2-5).  


Kostiantyn Diachenko

Check out AEM VLT Intellij plugin


Avatar

Community Advisor

Hi @Srikanth23696837zrx0,

Relying on events, event handlers etc will not work. Mainly because all of them are triggered after specific node/resource is removed - so it is too late to read any information.

This is actually what you already observed on your own.

You can try solution that base on WCMCommand, I have described it step by step in below topic, which is very similar to yours.

Please only be aware that above solution might not work in New Assets View.