Unused AEM Assets Cleanup

Published
In asset management, it becomes apparent that certain assets are no longer referenced with any pages. It's recommended to delete these unused images from asset repository. This not only decreases size of AEM instance but also improves search/query performance.

Using ACS AEM Commons

ACS AEM Commons provides a way to identity unused assets. Using Managed Controlled Process (MCP), you can generate a report that lists all assets for a specific folder, along with the pages where they are referenced and the number of references.
MCP provides list of processes that can be executed. The Renovator process, one of them, is used for moving or renaming content in bulk. Prior to execution, a Dry Run can be performed to identify all assets in a specific folder, including the pages they are referenced on and the number of references. In fact, Dry Run is sufficient for our use case.
Steps to execute the Renovator process:
  • Navigate to Tools > ACS AEM Commons > Managed Controlled Process.
  • Click on Start Process.
  • Select Renovator from the list of available processes.
  • Renovator Process:
    • Source: Path of the folder you want to check for unused assets.
    • Destination: Path where assets will be copied. However, since we're only performing a Dry Run, the path will not have any effect.MCP Renovator Process
    • Make sure to check the Dry run checkbox, otherwise all the assets will be moved to the Destination folder.MCP Renovator Dry Run
    • Detailed Report should be checked to view information such as the list of assets, the number of references, and the specific pages where each asset is referenced. Otherwise, the response will only include the total number of assets and how many are referenced on other pages.
  • Click on Start to execute the Renovator process.
Once the process is complete, a report will be generated. The report will provide a list of all assets in the specified folder, along with the pages where they are referenced and the number of references. This information can be used to identify unused assets that can be safely deleted from the asset repository.
  • Click on the View to view the report. You can also Download the report in Excel format for further analysis.MCP Renovator Process Execution Details
  • The report will provide a list of all assets in the specified folder, along with the pages where they are referenced and the number of references. This information can be used to identify unused assets that can be safely deleted from the asset repository.MCP Renovator Detailed Report
    If you look carefully at the report, you will see that the ALL REFERENCES column shows the number of references for each asset, while the REFERRED IN column shows the pages where the asset is referenced. Based on above report, 4 assets are not referenced on any page. You can safely delete these assets from the asset repository.
You can manually delete the assets from the AEM instance. Make sure to unpublish the assets before deleting them. However, if you have a large number of assets to delete, it's more efficient to automate the deletion using a servlet, or Groovy script.

Bulk Asset Delete using Sling Servlet

The downloaded Excel report can be used as input for Sling Servlet to handle bulk delete operations. The servlet will read the Excel file and delete all the assets that are not referenced on any page. The servlet can be implemented as follows:
  • Create a Sling Servlet that accepts the Excel file as input.
  • Use Apache POI to read Excel file and extract the list of unused assets. Based on your excel report, you can update index to read asset path and reference count. Apache POI included in AEM Jar by default so you don't need to add any additional dependencies.
  • Use Sling Job to delete the assets in the background. This is important because deleting a large number of assets can take time and you don't want to block the request.
  • Using RepoInit, Create a Service User and provide necessary permission to delete assets from the asset repository.
  • Retrieve ResourceResolver using Service User mapping.
  • Finally, delete the assets using ResourceResolver.
Additionally, you can send an email notification once the job is completed mentioning the status of each asset deletion. You can find the sample implementation here: https://github.com/MahediSabuj/aem-commons/pull/1/files.
The above approach using ACS Commons reports and a custom Sling Servlet involves manual steps, first generating and downloading the Excel report, then uploading it as a request payload (e.g., via Postman) to trigger the deletion process.
As an automated alternative, you can implement a Custom Managed Controlled Process (MCP) and initiate it directly from the ACS Commons interface. This streamlines the workflow by eliminating manual intervention. We will discuss this in detail in one of the upcoming articles.
Write your Comment