Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

code for Version Purging

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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. 

View solution in original post

14 Replies

Avatar

Level 9

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)

 

Avatar

Administrator

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

Avatar

Level 4

@kautuksahni,

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

'

Avatar

Level 4

@ 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)

Avatar

Level 9

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

Avatar

Level 4

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

 

Avatar

Level 9

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

Jitendra

Sh1ju wrote...

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

 

 

Avatar

Level 9

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

Avatar

Level 4

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

Avatar

Level 9

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

Jitendra

Avatar

Level 9

Hi,

Have you tried purging versions using OOTB service?.

Day CQ WCM Version Purge Task

Jitendra

Avatar

Level 9

Hi,

If you are using AEM 6.0/6.1, here is another tool which could help you purging versions.

http://localhost:4502/etc/versioning/purge.html

Sh1ju wrote...

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

 

Avatar

Correct answer by
Employee Advisor

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. 

Avatar

Level 2

The wemblog code removes all newer versions except the base version, which is probably not what you want (keep the latest version only).

To get the code:

  • search for VersionManagerImpl in CRXDE, this will give you a path like /libs/wcm/core/install/cq-wcm-core-5.6.24.jar
  • create a package in package manager with that path, build it, download it, unzip it and extract the jar with "jar xf cq-wcm-core-5.6.24.jar"
  • download a Java "disassembler" like http://jd.benow.ca/
  • open the VersionManagerImpl.class file
  • look at the "purgeVersions" method to see how its done