empty_trash
Permanently delete all emails in the trash folder. Use dryRun to preview deletions first.
Instructions
Permanently delete all emails in the trash (Deleted Messages or Trash folder). Use dryRun: true to preview first.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dryRun | No | If true, preview how many emails would be deleted without deleting |
Implementation Reference
- lib/imap.js:1014-1061 (handler)The `emptyTrash` function searches common trash folders (e.g., "Deleted Messages", "Trash") and deletes all emails found within them. It supports a `dryRun` mode for testing.
export async function emptyTrash(dryRun = false, creds = null) { const t0 = Date.now(); // iCloud uses 'Deleted Messages'; Gmail uses '[Gmail]/Trash'; standard IMAP uses 'Trash' const trashFolders = ['Deleted Messages', '[Gmail]/Trash', 'Trash']; const client = createRateLimitedClient(creds); await client.connect(); let mailbox = null; for (const folder of trashFolders) { try { await client.mailboxOpen(folder); mailbox = folder; break; } catch (err) { if (!err.message.includes('Mailbox does not exist') && !err.message.includes('NONEXISTENT') && !err.message.includes('does not exist')) { await safeClose(client); throw err; } } } if (!mailbox) { await safeClose(client); throw new Error('No trash folder found — tried: ' + trashFolders.join(', ')); } const raw = await client.search({ all: true }, { uid: true }); const uids = Array.isArray(raw) ? raw : []; if (dryRun) { await safeClose(client); return { dryRun: true, wouldDelete: uids.length, mailbox }; } if (uids.length === 0) { await safeClose(client); return { deleted: 0, mailbox, timeTaken: ((Date.now() - t0) / 1000).toFixed(1) + 's' }; } let deleted = 0; for (let i = 0; i < uids.length; i += CHUNK_SIZE) { const chunk = uids.slice(i, i + CHUNK_SIZE); await client.messageDelete(chunk, { uid: true }); deleted += chunk.length; } await safeClose(client); return { deleted, mailbox, timeTaken: ((Date.now() - t0) / 1000).toFixed(1) + 's' }; }