How to filter unused Assets that are present in DAM | Community
Skip to main content
vineel_k
Level 2
June 25, 2023
Solved

How to filter unused Assets that are present in DAM

  • June 25, 2023
  • 5 replies
  • 2026 views

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.

 

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 Tanika02

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-detailed-report-of-the-assets-usages-8e60024e8fd4 

 

 

 

5 replies

Mohit_KBansal
Adobe Employee
Adobe Employee
June 25, 2023

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.

aanchal-sikka
Community Advisor
Community Advisor
June 25, 2023

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
kapil_rajoria
Community Advisor
Community Advisor
November 21, 2024

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?

Ravi_Pampana
Community Advisor
Community Advisor
June 25, 2023

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!
Tanika02
Tanika02Accepted solution
Level 7
June 25, 2023

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-detailed-report-of-the-assets-usages-8e60024e8fd4 

 

 

 

Jagadeesh_Prakash
Community Advisor
Community Advisor
June 26, 2023

@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();
}
}
}
}