Hi,
How to purge the asset version in aem programmatically?
Configured OOTB Day CQ WCM Version Manager configuration to purge the versions but in this case not all the versions getting purged atleast 1 version will be there in the asset.
Tried to get version programmatically and delete the versions but same issue here as well purge will work only if the versions are more than 1.
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory versionHistory = versionManager.getVersionHistory(assetPath);
Iterator<Version> versions = versionHistory.getAllVersions();
while (versions.hasNext()) {
Version version = versions.next();
String versionName = version.getName();
String versionPath = version.getPath();
if (!"jcr:rootVersion".equals(versionName)) {
versionHistory.removeVersion(versionName);
}
}
I want a code which deletes all the versions of asset. Just keep the original version.
I tried deleting the property "mix:versionable" from the asset which is working and deleting all the versions. But when I upload newer version again all the older versions are getting added. Not sure whether the version is getting deleted or the version is getting unlinked when I run this code.
Node assetNode = session.getNode(assetPath);
if (assetNode.isNodeType("mix:versionable")) {
assetNode.removeMixin("mix:versionable");
session.save();
}
Please let me know if this is the right way. If not please share the appropriate way to delete all the asset versions.
Thanks.
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @BrianKasingli ,
After removing the ref of version from asset then able to delete. I'm removing property "mix:versionable" and then deleted the versions. Can we do it this way?
session = resolver.adaptTo(Session.class);
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory versionHistory = versionManager.getVersionHistory(assetPath);
List<String> names = new ArrayList<>();
VersionIterator versionIterator = versionHistory.getAllVersions();
while (versionIterator.hasNext()) {
Version v = versionIterator.nextVersion();
String name = v.getName();
if (!name.equals("jcr:rootVersion")) {
names.add(v.getName());
}
}
Node assetNode = session.getNode(assetPath);
if (assetNode.isNodeType("mix:versionable")) {
assetNode.removeMixin("mix:versionable");
session.save();
}
for (String name : names) {
versionHistory.removeVersion(name);
}
Hi @Keerthana_H_N , as i understand ,you cannot purge every version of a versionable node programmatically using the JCR API ,there will always be at least one version (the root version) in the version history. This is by design, and is specified by the JCR and Oak implementation.I would recommend to stick with the “remove all but root version” logic and don’t try to delete jcr:rootVersion
.
Hi @Ekhlaque ,
I tried deleting all the versions but jcr:rootVersion using versionHistory. But I'm getting error while deleting the base version using this code.
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory versionHistory = versionManager.getVersionHistory(assetPath);
Iterator<Version> versions = versionHistory.getAllVersions();
while (versions.hasNext()) {
Version version = versions.next();
String versionName = version.getName();
String versionPath = version.getPath();
if (!"jcr:rootVersion".equals(versionName)) {
versionHistory.removeVersion(versionName);
}
}
javax.jcr.ReferentialIntegrityException: OakIntegrity0001: Unable to delete referenced node: ${asset_version_node}
Views
Replies
Total Likes
Could you please check if this helps https://aem4beginner.blogspot.com/how-to-remove-version-history-in-cq-aem
Hi @arunpatidar ,
I'm doing same approach
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory versionHistory = versionManager.getVersionHistory(assetPath);
Iterator<Version> versions = versionHistory.getAllVersions();
while (versions.hasNext()) {
Version version = versions.next();
String versionName = version.getName();
String versionPath = version.getPath();
if (!"jcr:rootVersion".equals(versionName)) {
versionHistory.removeVersion(versionName);
}
}
It'll delete all the versions except rootVersion and last version. And getting this exception while deleting javax.jcr.ReferentialIntegrityException: OakIntegrity0001: Unable to delete referenced node: ${asset_version_node}
Hi @arunpatidar ,
I want to delete all the versions except "jcr:rootVersion". That is the requirement.
We are creating version of assets to retain the metadata when we update the assets. Once the asset is updated and created a version. We want delete that version. Customer don't need the version of assets.
Hey @Keerthana_H_N ,
Take a look at the ACS Commons open-sourced Project. They have a servlet called DeletedContentVersionPurgerServlet https://github.com/Adobe-Consulting-Services/acs-aem-tools/blob/master/bundle/src/main/java/com/adob... that removes version histories of content that has already been deleted.
Hi @BrianKasingli ,
I have written the similar code and this will not delete all the versions. Error occurs while deleting baseVersion.
Views
Replies
Total Likes
I was expecting the ACS commons public code would be useful
Views
Replies
Total Likes
Hi @BrianKasingli ,
After removing the ref of version from asset then able to delete. I'm removing property "mix:versionable" and then deleted the versions. Can we do it this way?
session = resolver.adaptTo(Session.class);
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory versionHistory = versionManager.getVersionHistory(assetPath);
List<String> names = new ArrayList<>();
VersionIterator versionIterator = versionHistory.getAllVersions();
while (versionIterator.hasNext()) {
Version v = versionIterator.nextVersion();
String name = v.getName();
if (!name.equals("jcr:rootVersion")) {
names.add(v.getName());
}
}
Node assetNode = session.getNode(assetPath);
if (assetNode.isNodeType("mix:versionable")) {
assetNode.removeMixin("mix:versionable");
session.save();
}
for (String name : names) {
versionHistory.removeVersion(name);
}
if this works, I don't see why not.
@Keerthana_H_N Just checking in — were you able to resolve your issue? We’d love to hear how things worked out. If the suggestions above helped, marking a response as correct can guide others with similar questions. And if you found another solution, feel free to share it — your insights could really benefit the community. Thanks again for being part of the conversation!
Views
Replies
Total Likes
Hi @kautuk_sahni ,
Thanks for checking. I was able to resolve and I have marked the right answer.
Views
Likes
Replies