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
restoreTrashedSincebuilds a Drive search query that matches files which are trashed and were sent to trash aftersinceIso.- 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. - For each file in a page,
Drive.Files.updatesetstrashedback tofalse, which lifts the file out of trash and back to where it was. - 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.
restoreLast24his the everyday entry point: it works out a timestamp 24 hours ago and hands it torestoreTrashedSince.
Example run
Say four files were trashed yesterday afternoon and one was trashed last week
on purpose. Running restoreLast24h only touches the recent batch:
| File | Trashed | Restored? |
|---|---|---|
March report.gdoc | 18 hours ago | Yes |
Budget v3.xlsx | 18 hours ago | Yes |
Client photos.zip | 17 hours ago | Yes |
Notes.txt | 17 hours ago | Yes |
Old draft.gdoc | 6 days ago | No — 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:
- For a recent slip, select
restoreLast24hin the Apps Script editor and click Run. - For an older one, call
restoreTrashedSincewith a specific ISO timestamp, for example'2026-05-20T09:00:00Z', to set the start of the window. - Approve the authorisation prompt the first time.
- 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.
trashedTimeis 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
Archive a project folder when it's marked done
Zip and shelve completed Northwind work — keep active folders focused on in-flight projects.
Updated Dec 11, 2025
Build a shared-drive migration helper
Move Northwind files between drives with structure intact — from My Drive to a Shared Drive.
Updated Oct 8, 2025
Build a version-snapshot system for key files
Keep dated copies of Northwind's critical Sheets before risky edits.
Updated Sep 22, 2025
Pre-create dated archive folders
Generate per-month folders ahead of time so nothing lands in `misc/`.
Updated Sep 2, 2025
Move stale files to cold storage
Archive Northwind Drive files untouched for a year into a `cold storage` folder.
Updated Aug 1, 2025