Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 7

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 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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-...

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

Hope that helps!

Regards,

Santosh

View solution in original post

5 Replies

Avatar

Community Advisor

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/adob...

 

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

 

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.

Avatar

Correct answer by
Community Advisor

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-...

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

Hope that helps!

Regards,

Santosh

Avatar

Community Advisor

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.

 

Avatar

Level 7

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

Avatar

Community Advisor

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