Under normal circumstances, when an item in Box is deleted, it is not actually erased immediately. Instead, it is moved to the Trash. The Trash allows you to recover files and folders that have been deleted. By default, items in the Trash will be purged after 30 days.
- Get Trashed Items
- Get Trashed File Information
- Get Trashed Folder Information
- Permanently Delete File From Trash
- Permanently Delete Folder From Trash
- Restore a File from the Trash
- Restore a Folder from the Trash
The BoxTrash
implements Iterable<BoxItem.Info>
, so to get
the collection of items currently in the trash, simply iterate over it.
BoxTrash trash = new BoxTrash(api);
for (BoxItem.Info itemInfo : trash) {
// Process the item
}
Ordinarily, trying to call [getInfo()
][file-get-info] on a file that is in
the trash will return a 404 error. To get the information of a file in the
trash, you must instead call
BoxTrash#getFileInfo(String fileID)
with the ID of the trashed file. You can optionally
pass a specific list of fields to retrieve to getFileInfo(String fileID, String... fields)
,
which will return only the specified fields to reduce payload size.
String fileID = "9873459";
BoxTrash trash = new BoxTrash(api);
BoxFile.Info fileInfo = trash.getFileInfo(fileID);
Ordinarily, trying to call [getInfo()
][folder-get-info] on a folder that is in the trash will return a 404 error.
To get the information of a folder in the trash, you must instead call
BoxTrash#getFolderInfo(String fileID)
with the ID of the trashed folder. You can optionally
pass a specific list of fields to retrieve to
getFileInfo(String folderID, String... fields)
, which will return only the specified
fields to reduce payload size.
String folderID = "2345343";
BoxTrash trash = new BoxTrash(api);
BoxFolder.Info folderInfo = trash.getFolderInfo(folderInfo);
To delete a file from the trash, call BoxTrash#deleteFile(String fileID)
with the ID of the file to
delete.
Note: This will permanently delete the file, and cannot be undone.
String fileID = "87398";
BoxTrash trash = new BoxTrash(api);
trash.deleteFile(fileID);
To delete a folder from the trash, call BoxTrash#deleteFolder(String fileID)
with the ID of the
folder to delete.
Note: This will permanently delete the folder, and cannot be undone.
String folder = "123456";
BoxTrash trash = new BoxTrash(api);
trash.deleteFolder(folderID);
To restore a file from the trash, effectively undeleting it, call BoxTrash#restoreFile(String fileID)
with the ID of the file. To avoid scenarios where the parent folder that previously contained the file is no longer available,
the user does not have permission to create items in that folder, or that folder has an item with the same name as the one
being restored; you can pass a new parent folder ID and/or file name to
BoxTrash#restoreFile(String fileID, String newName, String newParentID)
. The new name
and parent will only be used if a conflict is encountered while trying to restore the file to its original location.
String fileID = "125367";
String newName = "Presentation 2018 ORIGINAL.pptx";
String newParentID = "98765";
BoxTrash trash = new BoxTrash(api);
// Avoid conflicts at the original location
trash.restoreFile(fileID, newName, newParentID);
To restore a folder from the trash, effectively undeleting it, call
BoxTrash#restoreFolder(String folderID)
with the ID of the folder. To avoid scenarios where the
parent folder that previously contained the folder to be restored is no longer available, the user
does not have permission to create items in that folder, or that folder has an item with the same name as the one
being restored; you can pass a new parent folder ID and/or folder name to
BoxTrash#restoreFolder(String folderID, String newName, String newParentID)
. The new name
and parent will only be used if a conflict is encountered while trying to restore the folder to its original location.
String folderID = "125367";
String newName = "My Documents ORIGINAL";
String newParentID = "98765";
BoxTrash trash = new BoxTrash(api);
// Avoid conflicts at the original location
trash.restoreFolder(folderID, newName, newParentID);