Skip to content

Instantly share code, notes, and snippets.

@pan-long
Last active April 15, 2019 15:00
Show Gist options
  • Save pan-long/17f14949fddc1cebd2863b8f4194a8ea to your computer and use it in GitHub Desktop.
Save pan-long/17f14949fddc1cebd2863b8f4194a8ea to your computer and use it in GitHub Desktop.
Remove old Drive files unless it has "#Indef" in its description.
var INDEF_LABEL = '#Indef';
var RETENTION_DAYS = 365;
var RETENTION_DATE = new Date();
RETENTION_DATE.setDate(RETENTION_DATE.getDate() - RETENTION_DAYS);
function DriveIndef() {
var root = DriveApp.getRootFolder();
RemoveOldFiles(root);
Drive.Files.emptyTrash();
}
function RemoveOldFiles(folder) {
if (folder.getDescription() !== null && folder.getDescription().indexOf(INDEF_LABEL) != -1) {
return;
}
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
if (file.getLastUpdated() > RETENTION_DATE) {
continue;
}
if (folder.getDescription() !== null && file.getDescription().indexOf(INDEF_LABEL) != -1) {
continue;
}
file.setTrashed(true);
}
var subFolders = folder.getFolders();
while (subFolders.hasNext()) {
RemoveOldFiles(subFolders.next());
}
if (!folder.getFiles().hasNext() && !folder.getFolders().hasNext() && folder.getLastUpdated() < RETENTION_DATE) {
folder.setTrashed(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment