Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Limit the number of backup folders
function deleteOldSheets() {
var df = DriveApp.getFolderById("your-backup-folder-id");
var arr = new Array();
var folders = df.getFolders();
while( folders.hasNext() ) {
var f = folders.next();
arr.push( [ [ f.getId() ] , [ f.getLastUpdated() ] ] );
}
arr.sort( sortFunction );
for( var i=14 ; i<arr.length ; i++ )
DriveApp.getFolderById( arr[i][0] ).setTrashed( true );
};
function sortFunction( a , b ) {
var aDate = new Date(a[1]);
var bDate = new Date(b[1]);
if ( aDate === bDate ) return 0;
else if ( aDate < bDate ) return 1;
else return -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.