You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

高效批量删除Google Drive根目录下50万+图片文件的方法求助

高效批量删除Google Drive根目录50万+图片的方案

方法1:Google Drive API分批次异步删除(推荐)

直接调用Google Drive API,通过分页+批量请求规避配额与速率限制:

  • 操作步骤:
    1. 启用Google Drive API,创建服务账号并下载密钥JSON文件
    2. 用Python结合google-api-python-client库编写脚本:
      • 按MIME类型筛选根目录下的图片文件(如image/jpegimage/png
      • 单次批量删除100个文件(API批量操作上限),每批后暂停1-2秒避免触发限制
      • 记录已删除文件ID,支持断点续删
        核心代码片段:
    from google.oauth2 import service_account
    from googleapiclient.discovery import build
    import time
    
    SCOPES = ['https://www.googleapis.com/auth/drive']
    SERVICE_ACCOUNT_FILE = 'your-service-account-key.json'
    
    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    service = build('drive', 'v3', credentials=credentials)
    
    def delete_batch(file_ids):
        batch = service.new_batch_http_request()
        for file_id in file_ids:
            batch.add(service.files().delete(fileId=file_id))
        batch.execute()
    
    page_token = None
    deleted_count = 0
    batch_size = 100
    
    while True:
        response = service.files().list(
            q="'root' in parents and mimeType contains 'image/'",
            spaces='drive',
            fields='nextPageToken, files(id)',
            pageToken=page_token,
            pageSize=batch_size
        ).execute()
    
        file_ids = [file['id'] for file in response.get('files', [])]
        if not file_ids:
            break
    
        delete_batch(file_ids)
        deleted_count += len(file_ids)
        print(f"已删除 {deleted_count} 个文件")
        page_token = response.get('nextPageToken', None)
        time.sleep(1)
    
    print("删除完成")
    
    • 注意:需将Drive根目录共享给服务账号邮箱,赋予编辑权限

方法2:优化Drive网页端批量操作

无需写代码,通过网页端快捷键提升效率:

  • 在Drive搜索栏输入type:image,筛选所有图片文件
  • 选中第一个文件后按住Shift点击最后一个,一次性选中当前页所有文件(默认每页100个),移至垃圾箱
  • 处理完一页后刷新页面,重复操作直至完成

方法3:分段执行Google Apps Script

若想用Apps Script,通过分段执行绕过配额:

  • 编写脚本每次删除500个文件,用PropertiesService记录上次处理位置
  • 设置时间驱动触发器,每2小时自动执行一次,利用每日配额重置机制
    核心代码片段:
    function deleteBatchImages() {
        const batchSize = 500;
        const props = PropertiesService.getScriptProperties();
        const pageToken = props.getProperty('lastPageToken') || null;
    
        const files = DriveApp.searchFiles("'root' in parents and mimeType contains 'image/'", pageToken, batchSize);
        let count = 0;
        let newPageToken = null;
    
        while (files.hasNext() && count < batchSize) {
            const file = files.next();
            file.setTrashed(true);
            count++;
        }
    
        newPageToken = files.getNextPageToken();
        if (newPageToken) {
            props.setProperty('lastPageToken', newPageToken);
        } else {
            props.deleteProperty('lastPageToken');
            ScriptApp.getProjectTriggers().forEach(trigger => ScriptApp.deleteTrigger(trigger));
        }
        Logger.log(`本次删除 ${count} 个文件`);
    }
    

内容的提问来源于stack exchange,提问作者احمد القيسي

火山引擎 最新活动