listLargestDriveFiles(opts) finds the biggest files you own in Drive and returns them as row objects — name, MB, owner, updated, url — largest first, ready for Append an Object as a Sheet Row. It never deletes anything. {limit} defaults to 25. {folderId} limits the scan to that folder and its subfolders.
When to use
- A "what's eating my storage" tab you can sort and share
- Finding large files in one shared folder before you ask anyone to clean up
- Any script that should report sizes and leave the files alone
How to use this snippet
/**
* Return the largest files you own as {name, MB, owner, updated, url} objects,
* largest first. Uses DriveApp. Native Google files that report size 0 are
* skipped (they do not count as stored bytes). opts.limit defaults to 25.
* opts.folderId, if set, walks that folder recursively and keeps only files
* you own. This function does not delete, trash, or move files.
*
* @param {{limit?: number, folderId?: string}} [opts]
* @return {Array<Object>}
*/
function listLargestDriveFiles(opts) {
opts = opts || {};
var limit = opts.limit == null ? 25 : Number(opts.limit);
if (!(limit >= 1) || Math.floor(limit) !== limit) {
throw new Error('listLargestDriveFiles: limit must be an integer >= 1');
}
var folderId = opts.folderId == null ? '' : String(opts.folderId).trim();
var me = '';
try { me = String(Session.getEffectiveUser().getEmail() || '').trim().toLowerCase(); } catch (e) { me = ''; }
var top = []; // smallest-first once full, so top[0] is the cutoff
function consider_(row) {
if (!row) return;
if (top.length < limit) {
top.push(row);
if (top.length === limit) {
top.sort(function (a, b) { return a._bytes - b._bytes; });
}
return;
}
if (row._bytes <= top[0]._bytes) return;
top[0] = row;
top.sort(function (a, b) { return a._bytes - b._bytes; });
}
function ownedByMe_(file) {
if (!me) return true;
try {
var o = file.getOwner();
var email = o && o.getEmail ? String(o.getEmail() || '').trim().toLowerCase() : '';
return email === me;
} catch (err) {
return false;
}
}
function fileRow_(file) {
var bytes = 0;
try { bytes = Number(file.getSize()) || 0; } catch (err) { bytes = 0; }
if (bytes <= 0) return null;
var owner = '';
try {
var o = file.getOwner();
owner = o && o.getEmail ? String(o.getEmail() || '') : '';
} catch (err2) { owner = ''; }
var updated = '';
try { updated = file.getLastUpdated(); } catch (err3) { updated = ''; }
var url = '';
try { url = file.getUrl(); } catch (err4) { url = ''; }
return {
name: file.getName(),
MB: Math.round(bytes / (1024 * 1024) * 100) / 100,
owner: owner,
updated: updated,
url: url,
_bytes: bytes
};
}
function walkFolder_(folder) {
var files = folder.getFiles();
while (files.hasNext()) {
var f = files.next();
if (!ownedByMe_(f)) continue;
consider_(fileRow_(f));
}
var subs = folder.getFolders();
while (subs.hasNext()) walkFolder_(subs.next());
}
if (folderId) {
var folder = DriveApp.getFolderById(folderId);
walkFolder_(folder);
} else {
var it = DriveApp.searchFiles("'me' in owners and trashed = false");
while (it.hasNext()) consider_(fileRow_(it.next()));
}
top.sort(function (a, b) { return b._bytes - a._bytes; });
return top.map(function (row) {
return {
name: row.name,
MB: row.MB,
owner: row.owner,
updated: row.updated,
url: row.url
};
});
}
Example
function reportLargestFiles() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Drive usage') ||
SpreadsheetApp.getActive().insertSheet('Drive usage');
sheet.clearContents();
sheet.getRange(1, 1, 1, 5).setValues([['name', 'MB', 'owner', 'updated', 'url']]);
var files = listLargestDriveFiles({ limit: 25 });
files.forEach(function (f) { appendObjectAsRow(sheet, f); });
}
The header row must match those keys exactly, including MB.
Notes
- A first scan of a large Drive can run long enough to hit the six-minute limit. Start with
{folderId}or a modestlimitif that happens; a timeout means the list may be incomplete. MBis rounded to two decimals. Files under about 5 KB show0.- The script has no delete path. Anything you later trash, you trash yourself from the sheet's URLs.
Happy Coding!
