Trash Files Older Than

trashFilesOlderThan(folderId, days, opts) looks at the files in one folder (never subfolders) and trashes those whose last updated time is strictly older than days. Pass {prefix: 'tmp-'} to limit the names, and leave dryRun at its default (true) to get a list of what would be trashed without moving anything.

When to use

  • Cleaning up dated PDFs or CSVs a script wrote into a temp folder
  • A scheduled sweep that should not walk the rest of Drive
  • Reviewing the list first, then running the same call with dryRun: false

How to use this snippet

/** * Trash files in one Drive folder that were last updated more than * `days` ago. Never recursive. `opts.prefix` limits names (prefix match). * `opts.dryRun` defaults to true and only reports. Returns * {dryRun, days, cutoff, prefix, results} where each result is * {name, id, url, lastUpdated, action, error}. * action is 'would-trash', 'trashed', or 'unavailable'. * * @param {string} folderId * @param {number} days * @param {{prefix?: string, dryRun?: boolean}} [opts] * @return {{dryRun: boolean, days: number, cutoff: Date, prefix: string, results: Array}} */ function trashFilesOlderThan(folderId, days, opts) { opts = opts || {}; var dryRun = opts.dryRun !== false; // default true var prefix = opts.prefix == null ? '' : String(opts.prefix); var n = Number(days); if (!(n >= 0) || n !== n) throw new Error('trashFilesOlderThan: days must be a number >= 0'); var cutoff = new Date(Date.now() - n * 86400000); var folder = DriveApp.getFolderById(folderId); var results = []; var files = folder.getFiles(); // this folder only, never recursive while (files.hasNext()) { var file = files.next(); var name = file.getName(); if (prefix && name.indexOf(prefix) !== 0) continue; var lastUpdated = file.getLastUpdated(); if (lastUpdated >= cutoff) continue; var row = { name: name, id: file.getId(), url: file.getUrl(), lastUpdated: lastUpdated, action: dryRun ? 'would-trash' : 'trashed', error: '' }; if (!dryRun) { try { file.setTrashed(true); } catch (e) { row.action = 'unavailable'; row.error = e.message; } } results.push(row); } return { dryRun: dryRun, days: n, cutoff: cutoff, prefix: prefix, results: results }; }

Example

function previewOldTempFiles() { var report = trashFilesOlderThan('FOLDER_ID', 14, { prefix: 'tmp-', dryRun: true }); Logger.log(report.results.map(function (r) { return r.action + ' ' + r.name; }).join('\n')); } function trashOldTempFiles() { // Only after you have reviewed a dry-run list for this folder. return trashFilesOlderThan('FOLDER_ID', 14, { prefix: 'tmp-', dryRun: false }); }

Notes

  • Age is getLastUpdated(), not the created date. A file you overwrite stays young; a file you have not touched ages out.
  • The helper does not enter subfolders and does not trash folders. If your script files dated exports into a child folder, point this at that child, or they will be left alone.
  • dryRun defaults to true. You must pass dryRun: false to move anything to trash. Trashed files stay in Drive trash until they are emptied, so this is reversible there, not a permanent delete.
  • If setTrashed fails (common on some shared-drive items where DriveApp cannot change the file), the row is unavailable with the error text and the file is left as-is.
  • For a non-destructive list of recent files, see List Drive Files Modified Since.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension