Google Drive SDK

Files: trash

Requires authorization

Moves a file to the trash. Try it now or see an example.

Request

HTTP request

POST https://www.googleapis.com/drive/v2/files/fileId/trash

Parameters

Parameter name Value Description
Path parameters
fileId string The ID of the file to trash.

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.apps.readonly

Request body

Do not supply a request body with this method.

Response

If successful, this method returns a Files resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File

import java.io.IOException;
// ...

public class MyClass {

  // ...

  /**
   * Move a file to the trash.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to trash.
   * @return The updated file if successful, {@code null} otherwise.
   */
  private static File trashFile(Drive service, String fileId) {
    try {
      return service.files().trash(fileId).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
    return null;
  }

  // ...

}

.NET

Uses the .NET client library

using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;

using System.Net;
// ...

public class MyClass {

  // ...

  /// <summary>
  /// Move a file to the trash.
  /// </summary>
  /// <param name="service">Drive API service instance.</param>
  /// <param name="fileId">ID of the file to trash.</param>
  /// <returns>The updated file, null is returned if an API error occurred</returns>
  public static File TrashFile(DriveService service, String fileId) {
    try {
      return service.Files.Trash(fileId).Fetch();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
    return null;
  }

  // ...
}

PHP

Uses the PHP client library

/**
 * Move a file to the trash.
 *
 * @param Google_DriveService $service Drive API service instance.
 * @param String $fileId ID of the file to trash.
 * @return Google_DriveFile The updated file. NULL is returned if an API error occurred.
 */
function trashFile($service, $fileId) {
  try {
    return $service->files->trash($fileId);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}

Python

Uses the Python client library

from apiclient import errors
# ...

def trash_file(service, file_id):
  """Move a file to the trash.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to trash.

  Returns:
    The updated file if successful, None otherwise.
  """
  try:
    return service.files().trash(fileId=file_id).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

Ruby

Uses the Ruby client library

##
# Move a file to the trash
#
# @param [Google::APIClient] client
#   Authorized client instance
# @param [String] file_id
#   ID of the file to trash
# @return [Google::APIClient::Schema::Drive::V2::File]
#   The updated file if successful, nil otherwise
def trash_file(client, file_id)
  drive = client.discovered_api('drive', 'v2')
  result = client.execute(
    :api_method => drive.files.trash,
    :parameters => { 'fileId' => file_id })
  if result.status == 200
    return result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
end

JavaScript

Uses the JavaScript client library

/**
 * Move a file to the trash.
 *
 * @param {String} fileId ID of the file to trash.
 */
function trashFile(fileId) {
  var request = gapi.client.drive.files.trash({
    'fileId': fileId
  });
  request.execute(function(resp) { });
}

Go

Uses the Go client library

import (
  "code.google.com/p/google-api-go-client/drive/v2"
  "fmt"
)

// TrashFile moves a file to the trash
func TrashFile(d *drive.Service, fileId string) (*drive.File, error) {
  r, err := d.Files.Trash(fileId).Do()
  if err != nil {
    fmt.Printf("An error occurred: %v\n", err)
    return nil, err
  }
  return r, nil
}

Objective-C

Uses the Objective-C client library

#import "GTLDrive.h"
// ...

+ (void)trashFileWithService:(GTLServiceDrive *)service
                      fileId:(NSString *)fileId
             completionBlock:(void (^)(GTLDriveFile *, NSError *))completionBlock {
  GTLQueryDrive *query = [GTLQueryDrive queryForFilesTrashWithFileId:fileId];
  // queryTicket can be used to track the status of the request.
  GTLServiceTicket *queryTicket =
    [service executeQuery:query
        completionHandler:^(GTLServiceTicket *ticket, GTLDriveFile *file,
                            NSError *error) {
          if (error == nil) {
            completionBlock(file, nil);
          } else {
            NSLog(@"An error occurred: %@", error);
            completionBlock(nil, error);
          }
        }];
}

// ...

Try it!

Use the APIs Explorer below to call this method on live data and see the response.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.