[AEM6.5] Servlet to Unpublish and Removing Bulk Assets [using path from CSV] | Community
Skip to main content
tushaar_srivastava
Level 6
December 11, 2022
Solved

[AEM6.5] Servlet to Unpublish and Removing Bulk Assets [using path from CSV]

  • December 11, 2022
  • 2 replies
  • 1646 views

Hi All,

I have asked a question  https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-unpublish-and-removing-bulk-assets-using-path-from-csv/m-p/562317#M139982 [AEM6.5] Unpublish and Removing Bulk Assets [using path from CSV] from which I came across the best way to unpublish and then remove asset for one time will be the Groovy Script.
But there is change in requirement, is I have to write a servlet, which will read the assets path through CSV and then Unpublish and remove those assets from AEM instance.

I need help to correct the servlet or any other suggested psudo code: 

 

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.apache.sling.servlets.annotations.SlingServletPaths; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.Servlet; import javax.servlet.ServletException; import java.io.*; import java.util.ArrayList; import java.util.List; @Component(service = Servlet.class) @SlingServletPaths( value = {"/test/unpublish"} ) public class UnpublishAssetServlet extends SlingAllMethodsServlet { private static final long serialVersionUID = 1L; @Reference private PageManager pageManager; final Logger log = LoggerFactory.getLogger(UnpublishAssetServlet.class); @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { String filePath = "/tmp/assetPath.csv"; List<String> assetPaths = readFromCsv(filePath); for (String assetPath : assetPaths) { unpublishAsset(assetPath); deleteAsset(assetPath); } log.info("Successfully removed assets"); response.getWriter().write("Successfully removed assets"); } private List<String> readFromCsv(String filePath) { BufferedReader br = null; String line = ""; String cvsSplitBy = ","; List<String> assetPaths = new ArrayList<String>(); try { br = new BufferedReader(new FileReader(filePath)); while ((line = br.readLine()) != null) { String[] values = line.split(cvsSplitBy); String assetPath = values[0]; assetPaths.add(assetPath); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return assetPaths; } private void unpublishAsset(String assetPath) { Page page = pageManager.getPage(assetPath); if (page != null) { try { pageManager.unpublish(page); log.info("Successfully unpublished asset at path " + assetPath); } catch (WCMException e) { log.error("Error unpublishing asset at path " + assetPath); e.printStackTrace(); } } } private void deleteAsset(String assetPath) { try { pageManager.delete(assetPath, false); log.info("Successfully deleted asset at path " + assetPath); } catch (WCMException e) { log.error("Error deleting asset at path " + assetPath); e.printStackTrace(); } } }

 

Thanks 

@arunpatidar  @kautuk_sahni  @theo_pendle  @kiran_vedantam 

 

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 aanchal-sikka

Hello @tushaar_srivastava 

 

1. For publishing/unpublishing, we should use "com.day.cq.replication.Replicator" API. Example:

@Reference
private Replicator replicator;
replicator.replicate(session, ReplicationActionType.ACTIVATE, path);

 2. We are dealing with assets here, but using PageManager. May be I am missing a point, as why PageManager is needed. If its for deleting asset, then please use Resourceresolver.

For deleting any resource you can use, resourceResolver.delete(resource);

2 replies

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
December 11, 2022

Hello @tushaar_srivastava 

 

1. For publishing/unpublishing, we should use "com.day.cq.replication.Replicator" API. Example:

@Reference
private Replicator replicator;
replicator.replicate(session, ReplicationActionType.ACTIVATE, path);

 2. We are dealing with assets here, but using PageManager. May be I am missing a point, as why PageManager is needed. If its for deleting asset, then please use Resourceresolver.

For deleting any resource you can use, resourceResolver.delete(resource);

Aanchal Sikka
Avinash_Gupta_
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
December 11, 2022

Hi @tushaar_srivastava 

 

There is no need to use PageManager API as you are dealing with assets. As @aanchal-sikka has mentioned, use that code snippet to unpublish and delete the assets. Session object also provides you the option to remove the item.

 

session.removeItem("/content/dam/nextgen/Ehub-POD/image1.jpg")
if (session.hasPendingChanges()) {
    session.save();        
}