Delete X days older subfolders and files in Google Drive

I created a little script to delete subfolders & files in specific folders older than 30 days on Google Drive.It doesn’t permanently delete them, it just puts them in the Trash folder. It works quite well to delete subfolders & files from Google Drive.

function DeleteOldFiles() {
  var ManySubFolders = new Array(
    'FOLDER_ID_HERE',
    'FOLDER_ID_HERE',
    'FOLDER_ID_HERE'
  );

  var OldDays = 30 * 24 * 60 * 60 * 1000; // 30 Days

  for (var key in ManySubFolders) {
    var folder = DriveApp.getFolderById(ManySubFolders[key])
    const files = folder.getFiles();
      while (files.hasNext()){
      var file = files.next();

      if (new Date() - file.getLastUpdated() > OldDays) {
        file.setTrashed(true); // Places the file in the Trash folder
      }
    }
    const folders = folder.getFolders();
    while (folders.hasNext()){
      var folder = folders.next();
      if (new Date() - folder.getLastUpdated() > OldDays) {
        folder.setTrashed(true); // Places the file in the Trash folder
      }
    }

  }

}

To complete the script, and delete subfolder & files from Google Drive:
Change FOLDER_ID_HERE with the folder ID’s you see in Google Drive. You can get them by double-clicking the folder and the URL structure will be something like this: https://drive.google.com/drive/folders/dfgfdsdfdfghf78ghjn3hbgddfgHyTdfgfdgyz
If you have more folders, just keep adding the ID’s to the Array.

The last part is quite easy and it requires you to make the script run once a day. While the Apps Script editor is open, in the menu click Edit -> All your triggers and then set a specific time.

Leave a Reply

×

Your Cart

No products in the cart.
Start shopping

About Us

Royal Technologies is a System Integration house that supports Web Design, Web Hosting, Domain Registration, Search Engine Optimizing, IT Consulting, Remote or Telephone Support and Software Development. In simpler terms it’s an IT Company dealing in various aspects of implementation and maintenance of computerization. We have learnt from experience and have molded ourselves to be complete IT Company.

CONTACT US

Location

Copyright © 2008-2024 Royal Technologies. All rights reserved.
Development by Royal Technologies