Delete Files and Folders

You can move a DriveResource, such as file or folder, to or from the user's trash with the DriveResource.trash and DriveResource.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. Returns false 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:

  1. Request the resource metadata.
  2. Check if isTrashable is true.
  3. (Optional) Check if isTrashed is true. Calling trash on an already trashed resource has no effect.
  4. Call [DriveResource.trash] or [DriveResource.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:

 * Handles the metadata response. If file is trashable and not
 * already trashed, makes a request to trash the file.
final ResultCallback<MetadataResult> metadataCallback = new ResultCallback<MetadataResult>() {
    public void onResult(MetadataResult result) {
        // Verify that the metadata can be retrieved.
        if (!result.getStatus().isSuccess()) {
            showMessage("Problem while trying to retrieve the file metadata");
        // If a DriveResource is a folder it will only be trashable if all of its children
        // are also accessible to this app.
        if (result.getMetadata().isTrashable()) {
          if (result.getMetadata().isTrashed()) {
          } else {
        } else {
            showMessage("Resource is not owned by the user or is in the AppFolder.");

For a full working example, see our Trash/Untrash sample on GitHub.

Delete a file

Deleting a file bypasses trash and permanently deletes the file. A file can be deleted using the DriveResource.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:

driveFile = Drive.DriveApi.getFile(mGoogleApiClient,
// Call to delete file.

For a full working example, see our Delete sample on GitHub.

Enviar comentarios sobre…

Drive API for Android
Drive API for Android