code for Version Purging | Community
Skip to main content
Sh1ju
Level 4
January 30, 2016
Solved

code for Version Purging

  • January 30, 2016
  • 14 replies
  • 4688 views

Can you please share the code for Version Purging  using  VersionManager (com.day.cq.wcm.api)

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 Kunal_Gaba_

Sh1ju wrote...

@ Jitendera ,Its already versionable.please check the screen shot

 

 

The versionable mixin is applied on jcr:content node whereas in your code you are trying to do the operation on mm1 node which may not have the mixin. 

14 replies

Jitendra_S_Toma
Level 10
January 30, 2016

Hi,

Here is the code which does the purging. I have not tested this code. However, it seems quite good.

http://wemcode.wemblog.com/version-purging

Here is the configuration doc which explains important attributes needed for purging tool

https://docs.adobe.com/docs/en/aem/6-1/deploy/configuring/version-purging.html#Purge Versions Tool

Jitendra

Sh1ju wrote...

Can you please share the code for Version Purging  using  VersionManager (com.day.cq.wcm.api)

 

kautuk_sahni
Community Manager
Community Manager
February 1, 2016

As mentioned by Jitendera,

Option 1:-

Link:- http://wemcode.wemblog.com/version-purging

would help you in writing a code to do version purging.

//

VersionManager mgr = session.getWorkspace().getVersionManager();

// get version history
    VersionHistory vh = (VersionHistory) node.getParent().getParent();
//  VersionHistory vh = mgr.getVersionHistory(path);
    String id = vh.getIdentifier();
 
//  get the names of the versions
    List<String> names = new LinkedList<String>();
    VersionIterator vit = vh.getAllVersions();
    while (vit.hasNext()) {
        Version v = vit.nextVersion();
        if (!v.getName().equals("jcr:rootVersion")) {
           names.add(v.getName());
        }
    }
        
// remove all versions
    for (String name: names) {
      vh.removeVersion(name);
    }

 

option 2:- Using CURL

Link:- https://helpx.adobe.com/experience-manager/kb/curl-command-version-purge.html

//

Using cron job, schedule the curl commands [1], and then remove the progress file. The following list explains the various input parameters.

  • <cmd>  The value can be "dryrun" OR "purge."  The value "dryrun" helps to preview the purged versions. And, value "purge" launches the purge of the versions on the node that the path defines.
  • <maxdays> The maximum age of the version of a node. When the age of a version exceeds this value, it is purged.
  • <maxversions> The maximum number of versions to keep for a node. When this number exceeds this value, the oldest versions are purged.
  • <path> An absolute path on which the purge is applied.
  • <recursive> When purging data, you can choose between performing the operation on one node or on a whole hierarchy by selecting Recursive.

For more details, click here.

An example of curl command to purge everything except last five versions for the tree/content/geometrixx/en/test  is [2] & [3].

[1]

curl -u <userid>:<password> -f -o progress.txt -d "cmd=<cmd>&maxdays=<maxdays>&maxversions=<maxversions>&path=<path>&recursive=<recursive>" "http://<host>:<port>/etc/versioning/purge.html"

[2]

curl -u admin:admin -f -o progress.txt -d "cmd=purge&maxdays=0&maxversions=5&path=/content/geometrixx/en/test&recursive=true" "http://localhost:4502/etc/versioning/purge.html"

[3]

rm progress.txt

 

Reference Link:- https://cqwemblog.wordpress.com/2013/11/29/how-to-remove-page-version-under-a-content-tree/

I hope this would help you.

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
Sh1ju
Sh1juAuthor
Level 4
February 4, 2016

@kautuksahni,

I tried option [1],got an error 'org.apache.jackrabbit.core.NodeImpl cannot be cast to javax.jcr.version.VersionHistory'

'

Sh1ju
Sh1juAuthor
Level 4
February 4, 2016

@ Jitendera, I tried your solution but got an error

UnsupportedRepositoryOperationException javax.jcr.UnsupportedRepositoryOperationException: Unable to perform a versioning operation on a non versionable node: /content/geometrixx/mm/m1
    at org.apache.jackrabbit.core.version.VersionManagerImplBase.checkVersionable(VersionManagerImplBase.java:293)
    at org.apache.jackrabbit.core.version.VersionManagerImplBase.getVersionHistory(VersionManagerImplBase.java:354)
    at org.apache.jackrabbit.core.VersionManagerImpl.access$700(VersionManagerImpl.java:73)
    at org.apache.jackrabbit.core.VersionManagerImpl$4.perform(VersionManagerImpl.java:184)
    at org.apache.jackrabbit.core.VersionManagerImpl$4.perform(VersionManagerImpl.java:180)
    at org.apache.jackrabbit.core.session.SessionState.perform(SessionState.java:216)
    at org.apache.jackrabbit.core.VersionManagerImpl.perform(VersionManagerImpl.java:96)
    at org.apache.jackrabbit.core.VersionManagerImpl.getVersionHistory(VersionManagerImpl.java:180)
    at com.mm.online.cms.aem.archival.MMUtility.clearOldVersions(MMUtility.java:282)
    at com.mm.online.cms.aem.archival.impl.CleanUpServiceImpl.cleanup(CleanUpServiceImpl.java:260)
    at com.mm.online.cms.aem.archival.impl.CleanUpServiceImpl.run(CleanUpServiceImpl.java:228)
    at org.apache.sling.commons.scheduler.impl.QuartzJobExecutor.execute(QuartzJobExecutor.java:105)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:207)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Jitendra_S_Toma
Level 10
February 4, 2016

Have you enable this node to make versionable?. Just verify whether it has a property (mix:versionable ) for version. If not, please add and then try it.

Ideally, this property should be at template level so that you don't need to add manually at individual pages.

Jitendra

Sh1ju
Sh1juAuthor
Level 4
February 4, 2016

@ Jitendera ,Its already versionable.please check the screen shot

 

Jitendra_S_Toma
Level 10
February 4, 2016

Do you mind sharing your Version Purging code with me?. 

Jitendra

Sh1ju wrote...

@ Jitendera ,Its already versionable.please check the screen shot

 

 

Jitendra_S_Toma
Level 10
February 4, 2016

I didn't notice at first glance, But we are doing purging here. How does your code throws an exception which is more like "Not able to create version on a particular page."

Are you sure this exception is related to purging?.

Jitendra

Sh1ju
Sh1juAuthor
Level 4
February 4, 2016

@Jitendra please find the code

public void clearOldVersions(Session session,String nodePath,Logger logger)
{
     VersionManager versionManager = null;
     VersionHistory versionHistory = null;
     VersionIterator versionItr = null;
     
     logger.debug("Starting clearOldVersions..");
    try {
        versionManager = session.getWorkspace().getVersionManager();
    } catch (UnsupportedRepositoryOperationException e)
    {
        logger.error("UnsupportedRepositoryOperationException",e);
        
    } catch (RepositoryException e) {
        logger.error("RepositoryException",e);
    }
     
    try {
        versionHistory = versionManager.getVersionHistory(nodePath);
    } catch (UnsupportedRepositoryOperationException e) {
        logger.error("UnsupportedRepositoryOperationException",e);
    } catch (RepositoryException e) {
        logger.error("RepositoryException",e);
    }
  
     // below return all versions in order of creation date
    
    try {
        versionItr = versionHistory.getAllVersions();
    } catch (RepositoryException e) {
        logger.error("RepositoryException",e);
    }

     //Since we know MaxVersion number then we need to iterate it accordingly. 

     //  get the names of the versions
     List<String> names = new LinkedList<String>();
    
     while (versionItr.hasNext()) {
        Version v = versionItr.nextVersion();
        try {
            if (!v.getName().equals("jcr:rootVersion")) {
               versionHistory.removeVersion(v.getName());
               logger.debug("Removing the version of "+nodePath);
            }
        } catch (AccessDeniedException e) {
            logger.error("AccessDeniedException",e);
        } catch (ReferentialIntegrityException e) {
            logger.error("ReferentialIntegrityException",e);
        } catch (UnsupportedRepositoryOperationException e) {
            logger.error("UnsupportedRepositoryOperationException",e);
        } catch (VersionException e) {
            logger.error("VersionException",e);
        } catch (RepositoryException e) {
            logger.error("RepositoryException",e);
        }
     }
     
     logger.debug("clearOldVersions....completed");
}

Jitendra_S_Toma
Level 10
February 4, 2016

Give me some time. Will get back to you very soon. Trying to validate your code.

Jitendra