最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Efficient way to delete large number of files from root dir in Google drive - Stack Overflow

programmeradmin5浏览0评论

I'm trying to delete over 500,000 image files stored in the root directory of my Google Drive, but I’m facing several challenges and have tried a few approaches without success.

Manual deletion: attempted to manually delete the files through the Google Drive interface, but the sheer volume (500k+ images) makes this approach unfeasible and very slow.

Mounting Google drive and writing a script: I've tried mounting Google Drive to a local environment and running a Python script to delete the files, but the mounting failed due to permission issues or connectivity problems.

Google Apps script: I've used Google Apps Script to try deleting the files programmatically. However, Apps Script hits the daily quota limits or takes too long to execute due to the large number of files.

Does anyone have an efficient solution to batch delete a large number of files from the root directory of Google Drive? I would appreciate any insights or alternative methods that can speed up this process.

I'm trying to delete over 500,000 image files stored in the root directory of my Google Drive, but I’m facing several challenges and have tried a few approaches without success.

Manual deletion: attempted to manually delete the files through the Google Drive interface, but the sheer volume (500k+ images) makes this approach unfeasible and very slow.

Mounting Google drive and writing a script: I've tried mounting Google Drive to a local environment and running a Python script to delete the files, but the mounting failed due to permission issues or connectivity problems.

Google Apps script: I've used Google Apps Script to try deleting the files programmatically. However, Apps Script hits the daily quota limits or takes too long to execute due to the large number of files.

Does anyone have an efficient solution to batch delete a large number of files from the root directory of Google Drive? I would appreciate any insights or alternative methods that can speed up this process.

Share Improve this question edited Mar 27 at 4:56 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 27 at 4:14 احمد القيسياحمد القيسي 1 2
  • This is just my guess. I thought that there were 2 patterns. 1. Delete each file by the delete method of the Drive API. 2. Move all files into the trash box and empty the trash box. When the batch request is used, the files can be processed every 100. But, even when this is used, the daily quota might be over. So, I think that it might take several days to achieve your goal several days. – Tanaike Commented Mar 27 at 5:08
  • Why is there a vote to close this question? – Don Hatch Commented Mar 27 at 5:10
Add a comment  | 

1 Answer 1

Reset to default 0

Instead of using Google Apps Script, you can use the Google Drive API (https://console.cloud.google/apis/library/drive.googleapis) in Python to batch delete files efficiently.
here is the python script

from googleapiclient.discovery import build
from google.oauth2 import service_account

# load google drive api credentials
SCOPES = ['https://www.googleapis/auth/drive']
SERVICE_ACCOUNT_FILE = 'path_to_your_service_account.json'

creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)

def batch_delete_files():
    page_token = None
    files_deleted = 0

    while True:
        response = service.files().list(q="'root' in parents",
                                        fields="files(id)",
                                        pageSize=1000,
                                        pageToken=page_token).execute()
        
        files = response.get('files', [])
        if not files:
            break

        for file in files:
            try:
                service.files().delete(fileId=file['id']).execute()
                files_deleted += 1
                if files_deleted % 100 == 0:
                    print(f"{files_deleted} files deleted...")
            except Exception as e:
                print(f"Error deleting file {file['id']}: {e}")

        page_token = response.get('nextPageToken', None)
        if not page_token:
            break

batch_delete_files()
发布评论

评论列表(0)

  1. 暂无评论