Expand my Community achievements bar.

SOLVED

How to filter unused Assets that are present in DAM

Avatar

Level 2

I got requirement that to filter un-used assets present in dam

-> I have written servlet and triggered SQL-2 Query to fetch all assets from dam, I am getting the all dam assets.

 

My Code to fetch the assets

 

 

RowIterator rowIterator = queryResult.getRows();
while (rowIterator.hasNext()) {
Row row = rowIterator.nextRow();
//Here i am getting all the DAM assets
LOG.info("---assets--->"+row.toString());

 

 

 

Now i need to filter the assets that are not used in whole project.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @vineel_k - 

 

You can leverage the OOTB Asset Usage service to check if each asset is being used within the AEM project.

 

Here is the detailed example of how you can you achieve the same programmatically 

https://medium.com/@tanikaaggarwal02/how-to-easily-filter-out-unused-dam-assets-in-aem-get-the-detai... 

 

 

 

View solution in original post

6 Replies

Avatar

Employee Advisor

For your requirement, you can leverage the AssetReferenceSearch API which can give the details of Assets used in a page (node of type cq:Page).

 

See this discussion also.

Avatar

Community Advisor

Hello @vineel_k 

 

We can use "ACS Commons Renovator" to find unused assets.

 

The following blog shares the details on how we can find, unpublish and delete unused assets

https://kiransg.com/2021/11/10/aem-asset-repo-cleanup-acs-commons-renovator/


Aanchal Sikka

Avatar

Level 6

Thank you @aanchal-sikka this worked completely fine. Is there any way I can get the dates(last modified or published) of the of the assets in the excel sheet as well?

Avatar

Community Advisor

Hi, 

You can use ReferenceSearch API to find whether the given asset is used on the site 

ReferenceSearch referenceSearch = new ReferenceSearch();
referenceSearch.setExact(true);
referenceSearch.setHollow(true);
referenceSearch.setMaxReferencesPerPage(-1);
if(StringUtils.isNotBlank(searchPath)) { // Parent content path
 
referenceSearch.setSearchRoot(searchPath);
}
Map<String, Info> assetRefMap = referenceSearch.search(request.getResourceResolver(),assetPath);
If assetRefMap.size() is 0 then it is not being used on any page under searchPath 
Hope this helps!

Avatar

Correct answer by
Community Advisor

Hello @vineel_k - 

 

You can leverage the OOTB Asset Usage service to check if each asset is being used within the AEM project.

 

Here is the detailed example of how you can you achieve the same programmatically 

https://medium.com/@tanikaaggarwal02/how-to-easily-filter-out-unused-dam-assets-in-aem-get-the-detai... 

 

 

 

Avatar

Community Advisor

@vineel_k  You can use the OOTB feature as suggested by @Tanika02  but if you really want to from custom code follow below code and specify the particular path in "assetManager.listChildren("/content/dam");"

 

 

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(service = SomeService.class, immediate = true)
public class SomeService {

@Reference
private ResourceResolverFactory resourceResolverFactory;

public void filterUnusedAssets() {
ResourceResolver resourceResolver = null;
try {
// Acquire a ResourceResolver using the ResourceResolverFactory
Map<String, Object> authInfo = new HashMap<>();
authInfo.put(ResourceResolverFactory.SUBSERVICE, "your-service-name");
resourceResolver = resourceResolverFactory.getServiceResourceResolver(authInfo);

// Get the AssetManager
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);

// Get all assets under the DAM path
Iterator<Asset> assetIterator = assetManager.listChildren("/content/dam");

// Iterate through the assets and filter unused ones
while (assetIterator.hasNext()) {
Asset asset = assetIterator.next();
String assetPath = asset.getPath();

// Check if the asset is referenced
boolean isReferenced = assetManager.isAssetReferenced(assetPath);

if (!isReferenced) {
// Asset is unused, do something with it
System.out.println("Unused Asset Path: " + assetPath);
}
}
} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
} finally {
// Close the ResourceResolver
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
}