0

I had a script that ran every day at 5 am, that would move a specific file (data.xls) to the trash. However, since DocsList has been retired, the script no longer functions, and I'm having trouble updating it.

I've seen a couple of delete/setTrashed scripts posted here, but they all seem to function for an array of files, and i only want one specific file deleted.

I'm not a coder, and self-taught myself most of the small amount i have, so i need it as simple as possible (sorry.)

Any and all help or guidance is very appreciated. Thank you.

function myFunction() {
var files = DriveApp.getFilesByName('data');
 while (files.hasNext()) {
   var file = files.next();
   ID = file.getId(file)
   Drive.Files.remove(ID);
 }
}
Matthew E
  • 25
  • 1
  • 2
  • 6
  • Take a look at the following link: [How to update DocsList to DriveApp in my code](http://stackoverflow.com/a/29778417/2946873) – Alan Wells Jun 08 '15 at 14:12
  • files.next() returns a file object and can be set to trash directly without obtaining an ID. see edited answer below – ScampMichael Jun 08 '15 at 15:24

3 Answers3

3

I've seen a couple of delete/setTrashed scripts posted here, but they all seem to function for an array of files, and i only want one specific file deleted.

Simply put, to delete a single file you delete the first item in the list, what you are calling an array and what Google calls a file iterator.

Retrieving a file by name is going to return a list(iterator), even if it has only one file by that name, so you must treat the single item as the first item in the iterator and set that first item to trash.

Edit:

function myFunction() {
var files = DriveApp.getFilesByName('data');
 while (files.hasNext()) {
   files.next().setTrashed(true);
   }
  }

if you know that there is one and only one file by that name you could do something as simple as:

function myFunction() {
    DriveApp.getFilesByName('data').next().setTrashed(true);
       }
ScampMichael
  • 3,688
  • 2
  • 16
  • 23
  • Ok, that makes some sense. I think i'm still getting it wrong though 'code'function myFunction() { var files = DriveApp.getFilesByName('data'); while (files.hasNext()) { var file = files.next(); ID = file.getId(file) Drive.Files.remove(ID); } } – Matthew E Jun 08 '15 at 14:49
1

Since this is the first hit for Googling "apps script move file to trash", I found the following easy solution:

let file = DriveApp.getRootFolder().createFile('RIP file.txt', 'Good-bye, world ㅜㅜ');
file.setTrashed(true); // So the file is deleted after 30 days

File documentation

Dharman
  • 30,962
  • 25
  • 85
  • 135
Dr-Bracket
  • 4,299
  • 3
  • 20
  • 28
-1

I have workaround using DriveApp removeFile. Note this does not delete or trash the file in the user archive, but is no longer visible in the named folder.

removeFile(child)

Removes the given file from the root of the user's Drive. This method does not delete the file, but if a file is removed from all of its parents, it cannot be seen in Drive except by searching for it or using the "All items" view.

DriveApp.getFolderById(DriveApp.getFolderById(folderId)).removeFile(DriveApp.getFileById(fileId))
  • 2
    This method is now deprecated as per the documentation: https://developers.google.com/apps-script/reference/drive/drive-app#removefilechild – yago Mar 30 '21 at 15:11