appscript.dev
Automation Intermediate Drive

Bulk-restore accidentally trashed files

Recover everything trashed in a date range — Northwind's emergency-undo button.

Published Sep 14, 2025

Someone at Northwind selected a folder, hit delete, and only realised a minute later that it was the wrong folder. Drive’s trash is forgiving — nothing is gone yet — but restoring dozens of files one right-click at a time is slow and error-prone, and it is exactly the moment you do not want to be careful by hand.

This script is the emergency-undo button. Give it a starting time and it restores every file trashed since then in one pass, walking the trash page by page so it works whether five files went or five hundred. The convenience wrapper restoreLast24h covers the most common case — “I deleted something yesterday and I need it all back”.

What you’ll need

  • The Drive API enabled in Advanced Services. In the Apps Script editor, open Services, add Drive API, and keep the identifier as Drive.
  • The files must still be in trash, not permanently deleted. Drive empties trash automatically after 30 days, so this is a recovery tool with a deadline.
  • A rough idea of when the deletion happened, so you can pass a sensible start time and avoid restoring things that were trashed on purpose.

The script

// Milliseconds in a day — used to build the "last 24 hours" window.
const MS_PER_DAY = 86400000;

/**
 * Restores every file trashed at or after the given time. Walks the
 * trash one page at a time so it scales to large deletions.
 *
 * @param {string} sinceIso An ISO-8601 timestamp; files trashed after
 *   this are restored.
 */
function restoreTrashedSince(sinceIso) {
  // 1. Build a Drive query for files trashed after the cutoff time.
  const q = `trashed = true and trashedTime > '${sinceIso}'`;

  let pageToken;
  let restored = 0;

  // 2. Page through the matching files — the trash may not fit in one page.
  do {
    const res = Drive.Files.list({
      q,
      fields: 'nextPageToken,files(id,name,trashed)',
      pageToken,
    });

    // 3. Restore each file by clearing its trashed flag.
    for (const f of res.files || []) {
      Drive.Files.update({ trashed: false }, f.id);
      Logger.log('Restored ' + f.name);
      restored++;
    }

    pageToken = res.nextPageToken;
  } while (pageToken);

  // 4. Report the total so you can confirm it matches what was lost.
  Logger.log('Done — restored ' + restored + ' file(s).');
}

/**
 * Convenience wrapper — restores everything trashed in the last 24 hours.
 */
function restoreLast24h() {
  const since = new Date(Date.now() - MS_PER_DAY).toISOString();
  restoreTrashedSince(since);
}

How it works

  1. restoreTrashedSince builds a Drive search query that matches files which are trashed and were sent to trash after sinceIso.
  2. Because a big deletion can return more files than one API page holds, the script loops on nextPageToken — each pass fetches the next page until there is none left.
  3. For each file in a page, Drive.Files.update sets trashed back to false, which lifts the file out of trash and back to where it was.
  4. It logs each file by name as it goes and keeps a running count, so you get a visible record of exactly what was recovered.
  5. restoreLast24h is the everyday entry point: it works out a timestamp 24 hours ago and hands it to restoreTrashedSince.

Example run

Say four files were trashed yesterday afternoon and one was trashed last week on purpose. Running restoreLast24h only touches the recent batch:

FileTrashedRestored?
March report.gdoc18 hours agoYes
Budget v3.xlsx18 hours agoYes
Client photos.zip17 hours agoYes
Notes.txt17 hours agoYes
Old draft.gdoc6 days agoNo — outside the window

The log shows four Restored ... lines and then Done — restored 4 file(s). The deliberately-trashed Old draft.gdoc is left where it belongs.

Run it

This runs on demand — the moment you notice the mistake:

  1. For a recent slip, select restoreLast24h in the Apps Script editor and click Run.
  2. For an older one, call restoreTrashedSince with a specific ISO timestamp, for example '2026-05-20T09:00:00Z', to set the start of the window.
  3. Approve the authorisation prompt the first time.
  4. Read the log to confirm the restored count matches what you expected.

Act sooner rather than later — Drive purges trash after 30 days, and once a file is permanently deleted this script cannot bring it back.

Watch out for

  • This restores everything trashed in the window, including files trashed on purpose. Pick the narrowest start time that still covers the accident, and check the log afterwards.
  • Restored files return to their original location. If the parent folder was also trashed, the file may land somewhere unexpected — restore folders first, then their contents.
  • trashedTime is set by Drive when a file enters trash. A file trashed, restored, and re-trashed carries the latest trash time, so old re-trashed files can fall inside a recent window.
  • Files permanently deleted, or purged after 30 days, are gone for good — this script only works while they are still in trash.
  • A very large deletion can approach the script runtime limit. The paging loop handles big sets, but if it times out, narrow the window and run it in stages.

Related