You can remove Google Drive files and folders from both your MyDrive and shared drives. You have two options to do this: trashor delete.
You can move files and folders into the trash and then restore them (within 30days of trashing them). Deleting files and folders removes them permanently fromDrive. If you trash, restore, or permanently delete multiplefiles or folders at once, it might take time for you to notice the changes.
This guide explains how you can dispose of files in Drive.
Trash
To remove Drive files, you can move them to the trash. Files inthe trash are automatically deleted after 30 days. You can restore files fromyour trash before the 30-day period.
Only the file owner can trash a file, and other users can't view files in theowner's trash. If you attempt to trash a file you don't own, you receive aninsufficientFilePermissionserror. For more information, see Permissions.
To verify you're the file owner, call thefiles.get method with the fileId
and the fields
parameter set to the boolean ownedByMe
field. The ownedByMe
field isn't populated for files in shared drives because they're owned by theshared drive, not individual users. For further information about returningfields using the fields
parameter, see Return specific fields for afile.
If you're not the file owner but still want a copy of the trashed file, do oneof the following:
- Make a copy of the file.
- Contact the owner to have them restore it from the trash.
Move a file to the trash
To move a file to the trash, use thefiles.update method and set thetrashed
field to True
. To trash a shared drive file, you must also set thesupportsAllDrives
query parameter to True
. For more information, seeImplement shared drive support.
If successful, the responsebody contains aninstance of the files resource.
The following code sample shows how to use the fileId
to mark the file astrashed:
Python
body_value = {'trashed': True}response = drive_service.files().update(fileId="FILE_ID", body=body_value).execute()
Node.js
const body_value = { 'trashed': True};const response = await drive_service.files.update({ fileId: 'FILE_ID', requestBody: body_value, }); return response;
Replace FILE_ID with the fileId
of the file that you want totrash.
Determine a trashed file's properties
When a file is trashed, you can retrieve additionalfiles properties. You can use thefiles.get method and include thefollowing fields in the fields
parameter. For more information, see Returnspecific fields for a file.
The following fields are populated for all files:
trashed
: Whether the file was trashed, either explicitly or from a trashedparent folder. Note that while usingtrashed
with thefiles.update
method sets the file's status, thefiles.get
method retrieves the file'sstatus.explicitlyTrashed
: Whether the file was explicitly trashed, as opposed torecursively trashed, from a parent folder.
The following fields are only populated for files located within a shared drive:
trashedTime
: The time that the item was trashed in RFC3339date-time format. If you're using the previous Drive API v2version, this field is calledtrashedDate
.trashingUser
: If the file was explicitly trashed, the user who trashed it.
Recover a file from the trash
To recover a file from the trash, use thefiles.update method and set thetrashed
field to False
. To untrash a shared drive file, you also must setthe supportsAllDrives
query parameter to True
. For more information, seeImplement shared drive support.
If successful, the responsebody contains aninstance of the files resource.
The following code sample shows how to use the fileId
to mark the file asuntrashed:
Python
body_value = {'trashed': False}response = drive_service.files().update(fileId="FILE_ID", body=body_value).execute()
Node.js
const body_value = { 'trashed': False};const response = await drive_service.files.update({ fileId: 'FILE_ID', requestBody: body_value, }); return response;
Replace FILE_ID with the fileId
of the file that you want tountrash.
Empty trash
You can permanently delete all Drive files the user has moved tothe trash using thefiles.emptyTrash method. Toempty the trash of a shared drive, you must also set the driveId
queryparameter to the shared drive ID.
If successful, the responsebody contains an emptyinstance.
The following code sample shows how to use the fileId
to delete all files inthe trash:
Python
response = drive_service.files().emptyTrash().execute()
Node.js
const response = await drive_service.files.emptyTrash({ }); return response;
Delete
You can permanently delete a Drive file without moving it to thetrash. After you delete a file, anyone you've shared the file with loses accessto it. If you want others to retain access to the file, you can transferownership tosomeone else before deletion.
To delete a shared drive file, the user must have role=organizer
on the parentfolder. If you're deleting a folder, all descendants owned by the user are alsodeleted. For more information, see Permissions.
To permanently delete a user-owned file without moving it to the trash, use thefiles.delete method. To delete ashared drive file, you must also set the supportsAllDrives
query parameter toTrue
. For more information, see Implement shared drivesupport.
If successful, the responsebody contains anempty instance.
The following code sample shows how to use the fileId
to delete the file:
Python
response = drive_service.files().delete(fileId="FILE_ID").execute()
Node.js
const response = await drive_service.files.delete({ fileId: 'FILE_ID' }); return response;
Replace FILE_ID with the fileId
of the file that you want todelete.
Permissions
The following table shows the role permissions required to perform eachoperation. For a complete list of roles and the operations permitted by each,refer to Roles and permissions.
Permitted operation | owner | organizer | fileOrganizer | writer | commenter | reader |
---|---|---|---|---|---|---|
Move files and folders into the trash | ✔ | ✔ | ✔ | |||
Recover files and folders from the trash | ✔ | ✔ | ✔ | |||
Empty the trash | ✔ | ✔ | ||||
Permanently delete a file or folder | ✔ | ✔ | ||||
Delete files and folders in a shared drive [*] | ✔ |
Capabilities
A files resource contains a collectionof boolean capabilities
fields used to indicate whether an action can beperformed on a file.
To check the capabilities, call thefiles.get method with the fileId
and the fields
parameter set to the capabilities
field. For furtherinformation about returning fields using the fields
parameter, see Returnspecific fields for a file.
The following fields are populated for all files:
capabilities.canTrash
: Whether the current user can move this file totrash.capabilities.canUntrash
: Whether the current user can restore this filefrom trash.capabilities.canDelete
: Whether the current user can delete this file.capabilities.canRemoveChildren
: Whether the current user can removechildren from this folder. This is false when the item isn't a folder.
The following fields are only populated for files located within a shared drive:
capabilities.canTrashChildren
: Whether the current user can trash childrenof this folder. This is false when the item isn't a folder.capabilities.canDeleteChildren
: Whether the current user can deletechildren of this folder. This is false when the item isn't a folder.
File and folder limits
Drive files and folders, along with shared drive folders, havesome storage limits.
Generally, after the item limit is reached, the only way to create more space isto permanently delete items or use adifferent account. Moving files to the trash isn't enough to free up space.
For more information on file and folder limits, see the following:
- File and folder limits in files
- File and folder limits in shared drives
- Delete files in Google Drive
- Shared drive versus My Drive API differences
- Roles and permissions