How to delete a File in Google Drive?

后端 未结 5 688
既然无缘
既然无缘 2020-12-31 03:57

How do I write a Google Apps Script that deletes files?

This finds files:

var ExistingFiles = DocsList.find(fileName);

But Do

5条回答
  •  青春惊慌失措
    2020-12-31 04:28

    This code uses the DocsList Class which is now deprecated.

    try this :

    function test(){
    deleteDocByName('Name-of-the-file-to-delete')
    }
    
    function deleteDocByName(fileName){
      var docs=DocsList.find(fileName)
        for(n=0;n

    since you can have many docs with the same name I used a for loop to get all the docs in the array of documents and delete them one by one if necessary.

    I used a function with the filename as parameter to simplify its use in a script, use test function to try it.

    Note : be aware that all files with this name will be trashed (and recoverable ;-)

    About the last part of your question about keeping the most recent and deleting the old one, it would be doable (by reading the last accessed date & time) but I think it is a better idea to delete the old file before creating a new one with the same name... far more logical and safe !

提交回复
热议问题