Deleting File Versions

When a file is modified, Documents and Media creates a new file version and leaves the previous version intact. Over time, old file versions can accumulate and consume precious storage space. Fortunately, you can use the Documents and Media API to delete them. Note, however, that there’s no way to send file versions to the Recycle Bin—once you delete them, they’re gone forever.

You can delete file versions with the DLAppService method deleteFileVersion:

deleteFileVersion(long fileEntryId, String version)

See this method’s Javadoc for a description of the parameters.

Follow these steps to use deleteFileVersion to delete a file version:

  1. Get a reference to DLAppService:

    @Reference
    private DLAppService _dlAppService;
    

    For more information on this, see the section on getting a service reference in the getting started tutorial.

  2. Get the file entry ID and version for the file you want to delete. Since it’s common to delete a file version specified by the end user, you can extract these parameters from the request. This example does so via javax.portlet.ActionRequest and ParamUtil, but you can do this any way you wish:

    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
    String version = ParamUtil.getString(actionRequest, "version");
    
  3. Use the service reference to call the deleteFileVersion method with the file entry ID and version from the previous step:

    _dlAppService.deleteFileVersion(fileEntryId, version);
    

You can find the full code for this example in the deleteFileEntry method of Liferay DXP’s EditFileEntryMVCActionCommand class. This class uses the Documents and Media API to implement almost all the FileEntry actions that the Documents and Media app supports. Also note that this deleteFileEntry method, as well as the rest of EditFileEntryMVCActionCommand, contains additional logic to suit the specific needs of the Documents and Media app.

Identifying File Versions

Since there may be many versions of a file, it’s useful to identify programmatically old versions for deletion. You can do this with FileVersionVersionComparator.

The following example creates such a comparator and uses its compare method to identify old versions of a file. The code does so by iterating through each approved version of the file (fileVersion). Each iteration uses the compare method to test that file version (fileVersion.getVersion()) against the same file’s current version (fileEntry.getVersion()). If this comparison is greater than 0, then the iteration’s file version (fileVersion) is old and is deleted by deleteFileVersion:

FileVersionVersionComparator comparator = new FileVersionVersionComparator();

for (FileVersion fileVersion: fileEntry.getVersions(WorkflowConstants.STATUS_APPROVED)) {

    if (comparator.compare(fileEntry.getVersion(), fileVersion.getVersion()) > 0) {

        dlAppService.deleteFileVersion(fileVersion.getFileEntryId(), fileVersion.getVersion());
    }
}

Deleting Files

Deleting File Shortcuts

Deleting Folders

Moving Entities to the Recycle Bin

« Deleting FilesDeleting File Shortcuts »
Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0