Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

How to delete page from author and publisher through code

Avatar

Level 2

Hi

 

I want to delete multiple pages from author and publisher. can someone suggest how to achieve this through code.

 

I have used pagemanager.delete method but it is deleting only in author not in publishers.

 

Thanks

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 5

@Nimma05 

To delete multiple pages from both the author and publisher in Adobe AEM, 

  • Deactivate the pages on the author instance: This will remove the pages from the publisher. You can use the Replicator API with the Deactivate action type to programmatically unpublish pages.
  • Delete the pages from the author instance: After deactivation, you can use the PageManager.delete() method to remove the pages from the author instance.
  • Execute the deletion on the publisher instance: If you want to ensure that the pages are also deleted from the publisher, you can write a script to delete the pages and execute it on both the author and publisher instances individually
     
    Something like might help 
    // Inject the ResourceResolverFactory and Replicator services
    ResourceResolverFactory resolverFactory = sling.getService(ResourceResolverFactory.class)
    Replicator replicator = sling.getService(Replicator.class)
    
    ResourceResolver resolver = null
    try {
        // Obtain a resource resolver
        resolver = resolverFactory.getAdministrativeResourceResolver(null)
    
        // Get the PageManager and Replicator services
        PageManager pageManager = resolver.adaptTo(PageManager.class)
    
        // Specify the paths of the pages you want to delete
        String[] pagePaths = ["/content/mysite/page1", "/content/mysite/page2"] // Add your page paths here
    
        for (String pagePath : pagePaths) {
            // Deactivate the page
            replicator.replicate(resolver.adaptTo(Session.class), ReplicationActionType.DEACTIVATE, pagePath)
    
            // Delete the page from the author instance
            pageManager.delete(pageManager.getPage(pagePath), true)
        }

View solution in original post

14 Replies

Avatar

Level 8

Hi @Nimma05

If you want to remove the pages from the publisher just unpublish the page from author, it will automatically removes the page from publisher and later you can delete the page in author.

 

can you please explain the exact use case of deleting the pages through code?

 

Avatar

Level 2

Hi,

I have written the code to deactivate the page and delete. The code is working but the issue is when we are trying to deactivate (through code) multiple pages around thousands of pages in all the publisher it is taking time and instance is getting slow. I am trying to figure out is there any possibility to directly delete the pages from author and it should remove from publisher as well because in my case deactivation is taking so long. So, I am looking for direct step of deletion instead of deactivating page,so automatically it will remove the page from publisher. As how we manually delete the page from author so it will automatically remove from publisher s

Thanks,

 

 

Avatar

Level 8

Hi @Nimma05 ,

 

Write a groovy script to delete the page and execute it on both author and publisher individually, so that you can skip deactivation of the page.

Avatar

Community Advisor

Hi @nimm You can use the scheduled job approach and execute the deactivation in phases.

And each time you excute the business logic

1. You can check for the existing jobs if it has been started or not. For that you can use the below logic to check the job status

Job job = jobManager.getJobById(request.getParameter("jobId"));
if(null!=job) {
	float percent = (((float) job.getFinishedProgressStep()) / job.getProgressStepCount()) * 100;
	// based on the percentage you can define whether your next job should be triggered or not
}

2. You can even check what is the current ReplicationQueue status, how many entries are present in the queue.

API Documentation Reference - https://javadoc.io/doc/com.adobe.aem/aem-sdk-api/latest/com/day/cq/replication/ReplicationQueue.html

 

Hope you find this helpful!

Thanks

 

Avatar

Community Advisor

Hi Nimma,

 

To programmatically unpublish page from author to publish you can use Replicator API with Deactivate action type[1]

To delete pages from author and publish you can use PageManager API[2]

 

[1] https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/index.html?com/day/cq...

[2] https://developer.adobe.com/experience-manager/reference-materials/6-4/javadoc/com/day/cq/wcm/api/Pa...

 

Regards,

Peter

Avatar

Level 2

Hi @Peter_Puzanovs 

 

Can you please consider the below scenerio 

 

I have written the code to deactivate the page and delete. The code is working but the issue is when we are trying to deactivate (through code) multiple pages around thousands of pages in all the publisher it is taking time and instance is getting slow. I am trying to figure out is there any possibility to directly delete the pages from author and it should remove from publisher as well because in my case deactivation is taking so long. So, I am looking for direct step of deletion instead of deactivating page,so automatically it will remove the page from publisher. As how we manually delete the page from author so it will automatically remove from publisher s

Avatar

Community Advisor

@Nimma05 

 

OOTB, if a published page is being deleted, it will trigger deactivation request. TO avoid system from getting overwhelmed, please try following:

  • Perform operations in batches.
  • Deactivate first, assure the Replicate queues clear, then delete
    • This is important in the scenario where queues are blocked. You might be forced to clear replication queue. Now, page is author is deleted, but will continue to exist in publish. Creating discrepancy and further investigations

Aanchal Sikka

Avatar

Employee Advisor

if you delete a page via the pageManager API it should deactivate the page if it's activated.

Avatar

Level 8

If you're using Java to interact with your CMS's API and you're finding that the pagemanager.delete method only deletes pages from the author and not the publisher, it suggests that the method may be specific to the authoring environment and doesn't handle deletion from the publisher. To delete pages from both the author and publisher, you'll likely need to use the appropriate APIs provided by your CMS.

Here's a general approach you can take in Java:

  1. Identify the CMS or Platform: Determine what CMS or platform you're using for authoring and publishing content. Each may have its own API or method for managing content.

  2. Authentication: Ensure you have the necessary authentication credentials (API keys, OAuth tokens, etc.) to interact with the CMS's API.

  3. Use the Appropriate API: Depending on the platform, you'll need to use the correct API or method to delete pages. Look for documentation on how to delete pages programmatically. This might involve making HTTP requests to specific endpoints, using client libraries provided by the CMS, or interacting with the CMS's Java SDK if available.

  4. Loop Through Pages: If you need to delete multiple pages, you'll likely need to loop through a list of page IDs or some other identifier and delete each page individually using the API.

  5. Handle Errors: Make sure to handle errors gracefully. Check for errors returned by the API and handle them appropriately in your code.

Here's a simplified example using Java and the HttpClient class to make HTTP requests:

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class PageDeletionExample {
    public static void main(String[] args) {
        // Assuming you have a list of page IDs to delete
        int[] pageIds = {1, 2, 3};

        // Assuming you have authentication credentials
        String token = "YOUR_API_TOKEN";

        HttpClient client = HttpClient.newHttpClient();

        for (int pageId : pageIds) {
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("https://your-cms-api.com/pages/" + pageId))
                    .header("Authorization", "Bearer " + token)
                    .DELETE()
                    .build();

            try {
                HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
                if (response.statusCode() == 200) {
                    System.out.println("Page with ID " + pageId + " deleted successfully.");
                } else {
                    System.out.println("Failed to delete page with ID " + pageId + ". Status code: " + response.statusCode());
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

Replace "YOUR_API_TOKEN" with your actual API token or authentication method, and "https://your-cms-api.com/pages/" + pageId with the actual endpoint provided by your CMS's API for deleting pages.

Remember to refer to your CMS's documentation for the exact API endpoints and authentication methods. If your CMS provides a Java SDK, it's recommended to use that for interacting with the API as it may provide more convenient methods and error handling.

Avatar

Community Advisor

@Nimma05 , Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Avatar

Correct answer by
Level 5

@Nimma05 

To delete multiple pages from both the author and publisher in Adobe AEM, 

  • Deactivate the pages on the author instance: This will remove the pages from the publisher. You can use the Replicator API with the Deactivate action type to programmatically unpublish pages.
  • Delete the pages from the author instance: After deactivation, you can use the PageManager.delete() method to remove the pages from the author instance.
  • Execute the deletion on the publisher instance: If you want to ensure that the pages are also deleted from the publisher, you can write a script to delete the pages and execute it on both the author and publisher instances individually
     
    Something like might help 
    // Inject the ResourceResolverFactory and Replicator services
    ResourceResolverFactory resolverFactory = sling.getService(ResourceResolverFactory.class)
    Replicator replicator = sling.getService(Replicator.class)
    
    ResourceResolver resolver = null
    try {
        // Obtain a resource resolver
        resolver = resolverFactory.getAdministrativeResourceResolver(null)
    
        // Get the PageManager and Replicator services
        PageManager pageManager = resolver.adaptTo(PageManager.class)
    
        // Specify the paths of the pages you want to delete
        String[] pagePaths = ["/content/mysite/page1", "/content/mysite/page2"] // Add your page paths here
    
        for (String pagePath : pagePaths) {
            // Deactivate the page
            replicator.replicate(resolver.adaptTo(Session.class), ReplicationActionType.DEACTIVATE, pagePath)
    
            // Delete the page from the author instance
            pageManager.delete(pageManager.getPage(pagePath), true)
        }

Avatar

Level 5

@Nimma05 

Did you try any of the suggestions? Can you share once you implement this.

Avatar

Level 1

Hi All ,

Facing similar issue where in when trying to deactivate the content via  ReplicationActionType.DEACTIVATE it is causing performance issues as the content is very huge. Did u find a solution ? can u help if that is the case !

Planning on deactivating only the parent page but not the child content by using custom methods? Has any one tried it . if yes please help , thanks !

 

 

Avatar

Administrator

@Nimma05 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni