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

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

  • December 7, 2022
  • 4 replies
  • 5590 views

Hi Team,

 

We have a CSV having list of assets to be deleted from instance and the list is in bulk [100+ assets path]

Removal of bulk assets will be one time activity, so what could be the best optimized approach?

eg :

Path

/content/dam/test/test-a/image.png

/content/dam/test/test-a/test-image.jpeg

/content/dam/test/test-b/sample.png

/content/dam/test/test-c/image-delete.png

...100+ records

 

I was thinking for Groovy script, or servlet to read asset path from CSV and perform activity like unpublish and delete the asset. Any sample solution will be very helpful

Any input: what could be the optimized way to read the path from CSV and then unpublish and remove the asset.

 

I am quite new to groovy script learning how to manage unpublish and delete assets, till now I did some basic operation like reading and removing the assets.

def csvFile = new File('/path/to/csv/file.csv') def assetPaths = [] // Read the CSV File line by line csvFile.eachLine { line -> def path = line.split(',')[0] assetPaths.add(path) } // Iterate over the asset paths and remove the assets from AEM assetPaths.each { path -> def resource = resourceResolver.getResource(path) if(resource != null) { resource.delete() }

But maybe need help to review and how to accomodate unpublish before deleting.

or is this correct approach to remove bulk assets

 

Thanks

 

@arunpatidar  @kautuk_sahni  @briankasingli  @vijayalakshmi_s  @himanshu_singhal  @veenavikraman 

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 SantoshSai

Hi @tushaar_srivastava ,

I would choose Groovy Console which seems to be the best option. Since you described it's one time activity hence just perfect one to use Groovy Console.

Better than what you have already listed above - Implement Servlet

You can consider ACS Commons capabilities: Bulk Add, Update and Delete properties in AEM – without using Groovy console[0].

Similar requirement has been discussed over here: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/deletion-of-property-from-pages-and-asset/m-p/560336#M139393

[0]: https://kiransg.com/2021/12/10/bulk-add-update-and-delete-properties-in-aem-without-using-groovy-console/

Hope that helps!

Regards,

Santosh

4 replies

Kiran_Vedantam
Community Advisor
Community Advisor
December 7, 2022

Hi @tushaar_srivastava 

 

You need to write a custom service using Java and create an OSGi bundle.

 

ACS Util for CSV: https://github.com/Adobe-Consulting-Services/acs-aem-tools/blob/master/bundle/src/main/java/com/adobe/acs/tools/csv/impl/CsvUtil.java

 

https://adobe-consulting-services.github.io/acs-aem-commons/features/mcp-tools/data-importer/index.html

 

You can read the file in your custom java code like this: https://stackoverflow.com/questions/58877269/to-read-csv-xlsx-files-present-under-dam-in-aem

 

Then use the replication API to unpublish and later delete the asset.

 

Hope this helps.

 

Thanks,

Kiran Vedantam.

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
December 7, 2022

Hi @tushaar_srivastava ,

I would choose Groovy Console which seems to be the best option. Since you described it's one time activity hence just perfect one to use Groovy Console.

Better than what you have already listed above - Implement Servlet

You can consider ACS Commons capabilities: Bulk Add, Update and Delete properties in AEM – without using Groovy console[0].

Similar requirement has been discussed over here: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/deletion-of-property-from-pages-and-asset/m-p/560336#M139393

[0]: https://kiransg.com/2021/12/10/bulk-add-update-and-delete-properties-in-aem-without-using-groovy-console/

Hope that helps!

Regards,

Santosh

Santosh Sai
Mani_kumar_
Community Advisor
Community Advisor
December 8, 2022

Hi @tushaar_srivastava 

I agree with @santoshsai as it is one time activity, preferable is go with groovy.

Please find the sample code which i used in one of our project to read the CSV.

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import java.util.stream.Collectors;

csvUsersPath = "/content/dam/sample.csv";

Resource csvResource = resourceResolver.getResource(csvUsersPath);

if(null != csvResource){
Asset asset = csvResource.adaptTo(Asset.class);
if(null != asset){
Rendition rendition = asset.getOriginal();
if(null != rendition){
InputStream inputStream = rendition.getBinary().getStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
List<String> assetRecords = reader.lines().collect(Collectors.toList());
assetRecords.remove(0);

Session session = resourceResolver.adaptTo(Session.class);

for (String record : assetRecords) {
session.save();
activate(path);
}
}
}
}else {

}

Hope this is helpful.

 

tushaar_srivastava
Level 6
December 8, 2022

Thank you @kiran_vedantam . @santoshsai , @mani_kumar_  for your response, I am quite new to groovy script learning how to manage unpublish and delete assets, till now I did some basic operation like reading and removing the assets.

 

def csvFile = new File('/path/to/csv/file.csv')
def assetPaths = []

// Read the CSV File line by line
csvFile.eachLine { line ->
    def path = line.split(',')[0]
    assetPaths.add(path)
}

// Iterate over the asset paths and remove the assets from AEM
assetPaths.each { path ->
    def resource = resourceResolver.getResource(path)
    if(resource != null) {
        resource.delete()
    }
}

But maybe need help to review and how to accomodate unpublish before deleting.

or is this correct approach to remove bulk assets

Mani_kumar_
Community Advisor
Community Advisor
December 8, 2022

Hi @tushaar_srivastava 

All the methods that you use and import in java is also applicable in groovy script, so use same methods and logic to write the deactivation and deletion in the groovy script if your are new to groovy.

 

Hope this helps