On Deletion, how to get the resource details before it gets deleted? | Community
Skip to main content
Level 2
August 18, 2017
Solved

On Deletion, how to get the resource details before it gets deleted?

  • August 18, 2017
  • 10 replies
  • 6637 views

Hi,

On deletion of a resource, I need get a property from the resource BEFORE the resource becoming null.

I tried this through Event Listener. In the event listener, the resource is null.

I tried to achieve this through on delete -> launcher -> workflow.   Here also the resource is null. But in this workflow, I have only one processor which tries to get the resource details.

Here can I get the resource details before it gets deleted from the workflow? Actual deletion is done in OOTB code, how can move that code to my workflow?

Here How to take an action on a deleted Asset in Adobe Experience Manager? , Jörg said we can control when the actual deletion is called. But how?

thanks

Srini

Best answer by smacdonald2008

Write a custom workflow step. Then using the AEM Workflow APIs, you can get the path of the node that is deleted. You can use this API logic that writes out the path of the node that will be deleted.

public void execute(WorkItem item, WorkflowSession wfsession, MetaDataMap args) throws WorkflowException {

          

try

{

WorkflowData data = item.getWorkflowData();

String path = null;

String type = data.getPayloadType();

   if (data.getPayload() != null) {

       String payloadData = (String) data.getPayload();

       path = payloadData;

      

   }

log.info("*** THE path of the node to DELETED IS "+path);

}

  

    catch (Exception e)

    {

    log.info(" MASSIVE ERROR "+e.getMessage())  ;

    }

}

Then the next step in the workflow deletes the page. This is how you get information about JCR content - typically a page that is deleted.

Workflow model --

Hope this helps....

10 replies

Level 4
August 21, 2017

You can implement Preprocessor to fetch the data from resource before it's deletion. Remember Preprocessor works only on activated page as mentioned here.

Diego_Fontan
Level 4
August 21, 2017

You can create a Custom Event Listener Class when a node is removed it.

Basically, you have to add your action and response  when certain event occurs, in your case, it could be something like this ( javax.jcr.observation.Event.NODE_REMOVED)

Let me know if you need complete example, how to implemented

Best Regards

Diego

SrininAuthor
Level 2
August 22, 2017

Hi,

Here my case is, the content which I am deleting is unpublished content. So in this case, Preprocessor won't get triggered. Any other option I have?

smacdonald2008
Level 10
August 22, 2017

To solve this requirement, you need to do what Joerg suggests. You need to build a custom workflow step and in that custom step, read the Java node props using a Java API. This step would be invoked before the payload was deleted - which would be a later step.

I will add this use case to our HELPX articles list.

TIP - An event handler will not work as the Event Handler will be fired after the node is  deleted.

SrininAuthor
Level 2
August 22, 2017

Scott,

Thanks for your reply. Please let me know once the HELPX article is available. I already built a custom workflow with only one workflow step which triggers on node delete. But the resource is getting deleted by the time it comes to the workflow. Should I add one more step for deletion?

thanks

Srini

smacdonald2008
Level 10
August 22, 2017

I will look into this at code lvl tomorrow.

smacdonald2008
Level 10
August 23, 2017

I am going to show you how to perform this use case via a custom Workflow Step so we can read the information from the deleted node prior to deleting it. I will post back soon.

smacdonald2008
smacdonald2008Accepted solution
Level 10
August 23, 2017

Write a custom workflow step. Then using the AEM Workflow APIs, you can get the path of the node that is deleted. You can use this API logic that writes out the path of the node that will be deleted.

public void execute(WorkItem item, WorkflowSession wfsession, MetaDataMap args) throws WorkflowException {

          

try

{

WorkflowData data = item.getWorkflowData();

String path = null;

String type = data.getPayloadType();

   if (data.getPayload() != null) {

       String payloadData = (String) data.getPayload();

       path = payloadData;

      

   }

log.info("*** THE path of the node to DELETED IS "+path);

}

  

    catch (Exception e)

    {

    log.info(" MASSIVE ERROR "+e.getMessage())  ;

    }

}

Then the next step in the workflow deletes the page. This is how you get information about JCR content - typically a page that is deleted.

Workflow model --

Hope this helps....

Level 2
October 19, 2023

Hi, How can we trigger this workflow while a Node/Page is deleted. If I add a launcher for Removed for cq:Page, the workflow is triggered post the page is deleted.

Is there any way to trigger this workflow right before the page/node is deleted?

smacdonald2008
Level 10
August 23, 2017

This log file has this message -- /content/summit-toys/pt] com.aem.community.core.CustomDataStep *** THE TITLE OF THE DELETED ITEM IS /content/summit-toys/pt

This code works!

Level 3
July 11, 2025

Hi @srinin 

 

Were you able to resolve this issue? I tried through Event Listener, ResourceChangeListener, EventHandler and created a workflow and used a launcher to trigger the workflow, none of them worked. In all these cases, the resource is coming up as null.

Please let us know if you were able to resolve the issue.

 

TIA.