« Back

How to clean previous version of documents from Document and Media

General Blogs April 2, 2014 By Mitesh S Panchal

If you are using liferay sync you might have run in to issue where you end up having 1000 version of the same document and for 1 MB size document your disk space utilization 1 GB.  

Reason is for every save of document opened from folder mapped using liferay sync it will try to create a version if you are connected to the network and liferay sync is active. 

You can use this Groovy script to clean up  all previous version of the document to clean up the disk space.  Run this script from Control Panel > Server Administration > Script > Select Groovy .

Note: run it carefully this script will delete all the verison of the document for a given folder and it's children in given group/site.

 

import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import java.util.List;
 
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.repository.model.FileVersion;
import com.liferay.portal.kernel.repository.model.Folder;
import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
import com.liferay.portlet.documentlibrary.model.DLFileVersion;
 
// Param1: Group ID
// Param2: Folder ID
listFiles(16018, 9077187);
 
def listFiles(groupId, folderId) {
    allfiles = DLAppServiceUtil.getFileEntries(groupId,folderId)
    for (FileEntry file:allfiles) {
         System.out.println("File Title: " + file.getTitle());
         System.out.println("File Version: " + file.getVersion());
        List results = file.getFileVersions(-1);
         latestversion = file.getVersion();
         for(FileVersion fv : results){
             if(fv.getVersion() != latestversion){
                  System.out.println("Deleting >>" + fv.getVersion()  );
                  DLAppServiceUtil.deleteFileVersion(file.getFileEntryId(), fv.getVersion());
             }
         }
     }
 
     allfolders = DLAppServiceUtil.getFolders(groupId,folderId);
     for (Folder folder:allfolders) {
           System.out.println("Folder Name: " + folder.getName());
           listFiles(groupId,folder.getFolderId());
     }
}
Threaded Replies Author Date
It is really good. Thanks for your contribution. Varun Shah April 2, 2014 12:53 PM
Nice Mitesh, it was helpful. Advait Trivedi April 21, 2014 11:06 PM
Thanks for this; it really helped me do what I... Jerry Smith June 6, 2014 7:23 AM
Awesome script Mitesh! Thanks for sharing Thiago Leão Moreira April 8, 2015 10:50 PM
It is really good...................... Ramalingaiah. D August 2, 2016 12:00 AM

It is really good. Thanks for your contribution.
Posted on 4/2/14 12:53 PM.
Nice Mitesh, it was helpful.
Posted on 4/21/14 11:06 PM.
Thanks for this; it really helped me do what I wanted in Java.

One thing I had to research by searching through source code (wish the javadocs were better) that might be helpful to someone: the -1 you pass into the call file.getFileVersions is checked against "WorkflowConstants.STATUS_ANY" to get versions with any status.

I modified my code to use the constant to make it more clear since it won't just be a script.
Posted on 6/6/14 7:23 AM.
Awesome script Mitesh! Thanks for sharing
Posted on 4/8/15 10:50 PM.
It is really good......................
Posted on 8/2/16 12:00 AM.