You can move a DriveResource
, such
as file or folder, to or from the user's trash with the
DriveResourceClient.trash
and
DriveResourceClient.untrash
methods. These
can be used to trash or untrash files or folders owned by the currently
authenticated user. DriveResource
objects that are shared with but not owned
by the currently authenticated user cannot be trashed or untrashed.
If the target of the trash/untrash operation is a folder, all descendants of that folder are similarly trashed or untrashed. If your application does not have permission to all of the descendants of the target folder, the entire operation is canceled. If the folder contains items not owned by the current user, the folder is still trashed or untrashed, but items not owned by the current user are unaffected and remain in their current state.
Check if a file is in the trash
You can determine if a file is already in the trash or can be trashed by retrieving its metadata. There are three methods for determining a file's status:
Metadata.isTrashed()
- Returns
true
if the resource is already in the user's trash. Metadata.isTrashable()
- Returns
true
if the resource is owned by the currently authenticated user and is not in the AppFolder. The resource may still be untrashable if one of its descendents is not accessible by your application or is in the AppFolder. Metadata.isExplicitlyTrashed()
- Returns
true
if the resource was explicitly trashed. Returnsfalse
if the resource was trashed as a result of its parent folder being trashed.
Trash or untrash a file or folder
The typical workflow to trash or untrash a resource is as follows:
- Request the resource metadata.
- Check if
isTrashable
is true. - (Optional) Check if
isTrashed
is true. Callingtrash
on an already trashed resource has no effect. - Call
DriveResourceClient.trash
orDriveResourceClient.untrash
as applicable
The following code illustrates the code necessary to check the status of a file and toggle it between the trashed and untrashed state:
Delete a file
Deleting a file bypasses trash and permanently deletes the file. A file can be
deleted using the DriveResourceClient.delete
method. To delete a file, the authenticated user must be the owner of the file
and your app must have access to the file. If the resource being deleted is a
folder, your app must also have access to all descendants of the folder.
The following illustrates the code necessary to delete a file: