简体   繁体   English

如何使用 Google Drive API 一次删除多个文件

[英]How to delete multiple files at once using Google Drive API

I'm developing a python script that will upload files to a specific folder in my drive, as I come to notice, the drive api provides an excellent implementation for that, but I did encountered one problem, how do I delete multiple files at once?我正在开发一个 python 脚本,它将文件上传到我的驱动器中的特定文件夹,正如我注意到的,驱动器 api 为此提供了一个很好的实现,但我确实遇到了一个问题,我如何一次删除多个文件?
I tried grabbing the files I want from the drive and organize their Id's but no luck there... (a snippet below)我尝试从驱动器中抓取我想要的文件并组织它们的 ID,但没有运气......(下面的片段)

dir_id = "my folder Id"
file_id = "avoid deleting this file"

dFiles = []
query = ""

#will return a list of all the files in the folder
children = service.files().list(q="'"+dir_id+"' in parents").execute()

for i in children["items"]:
    print "appending "+i["title"]

    if i["id"] != file_id: 
        #two format options I tried..

        dFiles.append(i["id"]) # will show as array of id's ["id1","id2"...]  
        query +=i["id"]+", " #will show in this format "id1, id2,..."

query = query[:-2] #to remove the finished ',' in the string

#tried both the query and str(dFiles) as arg but no luck...
service.files().delete(fileId=query).execute() 

Is it possible to delete selected files (I don't see why it wouldn't be possible, after all, it's a basic operation)?是否可以删除选定的文件(我不明白为什么不可能,毕竟这是一项基本操作)?

Thanks in advance!提前致谢!

If you delete or trash a folder, it will recursively delete/trash all of the files contained in that folder. 如果您deletetrash文件夹,它会递归删除/回收站中的所有包含该文件夹中的文件。 Therefore, your code can be vastly simplified: 因此,您的代码可以大大简化:

dir_id = "my folder Id"
file_id = "avoid deleting this file"

service.files().update(fileId=file_id, addParents="root", removeParents=dir_id).execute()
service.files().delete(fileId=dir_id).execute()

This will first move the file you want to keep out of the folder (and into "My Drive") and then delete the folder. 这将首先将您要保留的文件移出文件夹(并进入“我的驱动器”),然后删除该文件夹。

Beware: if you call delete() instead of trash() , the folder and all the files within it will be permanently deleted and there is no way to recover them! 请注意:如果您调用delete()而不是trash() ,则文件夹及其中的所有文件将被永久删除,并且无法恢复它们! So be very careful when using this method with a folder... 因此在文件夹中使用此方法时要非常小心......

You can batch multiple Drive API requests together. 您可以一起批量处理多个Drive API请求 Something like this should work using the Python API Client Library : 这样的东西应该使用Python API客户端库

def delete_file(request_id, response, exception):
  if exception is not None:
    # Do something with the exception
    pass
  else:
    # Do something with the response
    pass

batch = service.new_batch_http_request(callback=delete_file)

for file in children["items"]:
  batch.add(service.files().delete(fileId=file["id"]))

batch.execute(http=http)

Files: delete Permanently deletes a file by ID. 文件:删除永久的ID删除文件。

Parameters 参数

fileId The ID of the file to delete. fileId要删除的文件的ID。

File.delete deletes a file as in one file at a time if you want to delete more then one you have to send a call for each file you want to delete File.delete一次删除一个文件中的文件如果要删除多个文件,则必须为要删除的每个文件发送一个调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM