Hi @divyat3
Using solutions that base on events will not work. Basically because events like Event.NODE_REMOVED, are triggered after specific node/resource is removed - so it is too late to read any information.
If you will observer how delete button is working, you can see that POST request is send to /bin/wcmcommand with parameter cmd set to deletePage - yes deletePage is used for both page and asset.
Having that knowledge you can utilize https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/commands/WCMCommand.html to register your own command that will be used whenever user clicks on Delete button. This will give you full control, and allow to run some activities before asset is removed.
Here is a sample code:
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.commands.WCMCommand;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HtmlResponse;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.propertytypes.ServiceRanking;
import java.util.ArrayList;
import java.util.List;
@ServiceRanking(100)
@Component(service = WCMCommand.class, immediate = true)
public class CustomDeleteCommand implements WCMCommand {
@Reference(cardinality = ReferenceCardinality.MULTIPLE)
private List<WCMCommand> commands = new ArrayList<WCMCommand>();
private WCMCommand deleteCommand;
@Activate
@Modified
void activate(){
System.out.println("Activating CustomDeleteCommand");
for (WCMCommand command : commands) {
if (command.getCommandName().equalsIgnoreCase("deletePage")
&& !(command instanceof CustomDeleteCommand)) {
deleteCommand = command;
}
}
}
@Override
public String getCommandName() {
return "deletePage";
}
@Override
public HtmlResponse performCommand(WCMCommandContext wcmCommandContext, SlingHttpServletRequest slingHttpServletRequest, SlingHttpServletResponse slingHttpServletResponse, PageManager pageManager) {
String path = slingHttpServletRequest.getParameter("path");
if (path != null && path.startsWith("/content/dam")) {
// place for you logic
}
// executing original delete command to remove page/asset
return deleteCommand.performCommand(wcmCommandContext, slingHttpServletRequest, slingHttpServletResponse, pageManager);
}
}
To be clear in above code we are registering new custom command that will be run instead of original OOTB one. But it is utilizing original command to run deletion process. Thanks to that you can easily combine your custom logic with OOTB behavior. Also you do not have to do any other modification on client side etc.
In case of any issue with above code try to restart AEM instance after installing above code.