Silviu
Silviu

Reputation: 23

Is there any way to trash a file using google drive api v3 for PHP?

I'm looking for a method to delete the file without having to permanently lose them from google drive using the API v3 for PHP... I mean, I want to trash it.

Looking at the V2 Reference I noticed that there is a direct function to do this, but in V3 it is no longer present and it doesn't work.

/**
* Move a file to the trash.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param String $fileId ID of the file to trash.
* @return Google_Servie_Drive_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;
} 

So, is there a way with Google Drive PHP API V3 to trash a file and/or folder without losing it permanently?

Upvotes: 2

Views: 729

Answers (1)

Tanaike
Tanaike

Reputation: 201603

I believe your goal and your current situation as follows.

  • You want to move the file and folder to the trash box using Drive API v3 with googleapis for PHP.
  • You have already been able to use Drive API v3 and your $service can be used for moving the file to the trash box.

In this case, the method of "Files: update" of Drive API v3 is used. The sample script is as follows.

Sample script:

$fileId = '###';  // Please set the file ID and folder ID.

$metadata = new Google_Service_Drive_DriveFile();
$metadata->setTrashed(true);
$res = $service->files->update($fileId, $metadata);

Reference:

Upvotes: 3

Related Questions