S
S
SerJik_Z2022-02-18 09:04:22
Google Drive
SerJik_Z, 2022-02-18 09:04:22

How to delete a certain type of files from Google Drive?

There is this script:

function getFilesByDate() {
  var arrayOfFileIDs = [];

  var ThirtyDaysBeforeNow = new Date().getTime() - 3600 * 1000 * 24 * 7;

  var cutOffDate = new Date(ThirtyDaysBeforeNow);
  var cutOffDateAsString = Utilities.formatDate(cutOffDate, 'GMT', 'yyyy-MM-dd');

  var theFileID = '';

  var files = DriveApp.searchFiles('modifiedDate < "' + cutOffDateAsString + '"');

  while (files.hasNext()) {
    var file = files.next();
    theFileID = file.getId();

    arrayOfFileIDs.push(theFileID);
    Logger.log('ID: ' + file.getId() + ', Name: ' + file.getName());
    Logger.log('Дата последнего обновления: ' + file.getLastUpdated());
  }

  return arrayOfFileIDs;
}

It allows you to find files older than 7 days.

How can I make it find files of a certain type older than n days and delete them immediately from the trash?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2022-02-18
@SerJik_Z

To have n days, pass the parameter instead of 7 here

var n = 7;
var ThirtyDaysBeforeNow = new Date().getTime()-3600*1000*24*n;

To indicate the type, pad here
var files = DriveApp.searchFiles(
     'modifiedDate < "' + cutOffDateAsString + 
     '" and (mimeType = "application/zip" or mimeType = "image/jpg")');

I think that there are some pseudo types that can combine image/jpgand image/jpeg, but not a fact.
To empty the recycle bin, call after deleting this
Drive.Files.emptyTrash();
You must enable an Advanced Service called Drive
620f43c3dc7cb754862703.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question