amorz
8/16/2016 - 6:54 AM

Auto delete Akeeba backup files. Google apps script. Install Google apps script in drive to use this file!

Auto delete Akeeba backup files. Google apps script. Install Google apps script in drive to use this file!

/*
Auto delete backupfiles in folder older than 30 days
Fill in the folder id found in the drive url
And keep a good structure with mulitple clients

- [Backups]
 -- client name
 -- client name
 -- client name

Stephan Zuidberg
www.amrz.nl

*/

function getSubFolders(parent) {
    parent = parent.getId();
    
    var childFolder = DriveApp.getFolderById(parent).getFolders();
    
    while (childFolder.hasNext()) {
    
        var child = childFolder.next();
        
        getSubFolders(child);
      
    }
    return;
}

function removeAkeebaBackupFiles() {
    
    var parentFolder = DriveApp.getFolderById("------FOLDER ID------");
    var childFolders = parentFolder.getFolders();

    while (childFolders.hasNext()) {
        
        var child = childFolders.next();

        getSubFolders(child);

        var folder = DriveApp.getFolderById(child.getId());
        var files = folder.getFiles();
        var ThirtyDaysBeforeNow = new Date().getTime() - 3600 * 1000 * 24 * 30;
        var cutOffDate = new Date(ThirtyDaysBeforeNow);
        var cutOffDateAsString = Utilities.formatDate(cutOffDate, "GMT", "yyyy-MM-dd");

        while (files.hasNext()) {
          
            var file = files.next();
            var getDateCreated = Utilities.formatDate(file.getDateCreated(), "GMT", "yyyy-MM-dd");

            if (cutOffDateAsString > getDateCreated) {
                
                try {
                    file.setTrashed(true);
                    Logger.log("Send to Trash: " + file.getName() + " Owner is " + file.getOwner());

                } catch (e) {
                    // Only owners can trash
                    Logger.log("Unable to Trash: " + file.getName() + "Owner is " + file.getOwner());
                }
               
            }
        }
    }
}