April 1, 2020
Estimated Post Reading Time ~

How to remove version history in CQ / AEM

Problem:
Over time the Version store /jcr:system/jcr:versionStorage can grow to a considerable size. You see that when:
lots of tar files in (CQ5.3):
/crx-quickstart/repository/version/copy
/crx-quickstart/repository/shared/version
large lucene index on:
/crx-quickstart/repository/repository/index

This is also helpful when you are upgrading from CQ5.2X to the latest version (As version purging was not enabled in the previous version)

Solution: There are a couple of solution to this,
You could enable automatic version purging. See more information here
You can also remove the version using the code mention below.

version purging
Do not use this code without this fix: https://issues.apache.org/jira/browse/JCR-2613 (fixed starting CRX 2.2)

Session session = resource.getResourceResolver().adaptTo(Session.class);
QueryManager qm = session.getWorkspace().getQueryManager();

String xpath = "";

xpath = "//element(*,nt:frozenNode)[jcr:like(cq:parentPath, '<some-path>%')]";

QueryResult result = qm.createQuery(xpath, javax.jcr.query.Query.XPATH).execute();
NodeIterator iter = result.getNodes();
Node node = null;

while (iter.hasNext()) {
node = iter.nextNode();
String path = node.getPath();
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);
}
}
session.logout();


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.