There are two methods you can use to delete files:
These methods differ only in how they identify a file for deletion. The
combination of the folderId
and title
parameters in deleteFileEntryByTitle
uniquely identify a file because it’s impossible for two files in the same
folder to share a name.
Follow these steps to delete a file:
-
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.
-
Get the data needed to populate the arguments of the
deleteFileEntry*
method you wish to use. Since it’s common to delete a file specified by the end user, you can extract the data you need from the request. This example does so viajavax.portlet.ActionRequest
andParamUtil
, but you can get the data any way you wish. Also note that this example gets only the file entry ID because it usesdeleteFileEntry
:long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
If you want to use
deleteFileEntryByTitle
instead, you can also get the repository ID, folder ID, and title from the request. For more information on getting repository and folder IDs, see the getting started tutorial’s sections on specifying repositories and folders. -
Call the service reference’s
deleteFileEntry*
method you wish to use with the data from the previous step. This example callsdeleteFileEntry
with the file entry’s ID:_dlAppService.deleteFileEntry(fileEntryId);
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.