How to delete multiple files at once using the Google Drive API

I am developing a python script that will upload files to a specific folder on my disk, as I already noticed, the api drive provides an excellent implementation for this, but I ran into one problem: how to delete several files at once?
I tried to grab the files that I want from the disk and organize their identifier, but there was no luck ... (fragment below)

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 ["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 understand why this would be impossible, because this is the main operation)?

Thanks in advance!

+4
source share
3 answers

API . - API Python:

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)
+2

delete trash, / , . :

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()

, ( " " ), .

: delete() trash(), , ! ...

+2

: a .


fileId .

File.delete , , , , .

-1
source

Source: https://habr.com/ru/post/1615751/


All Articles