Google Apps Script - Gmail, delete forever e-mails in trash with specific label
@karan's answer already points to the solution which worked for me, but being inexperienced / not-a-professional-developer, it took me a little work to translate it into a working solution to the original question. Here's a concise description of the steps I used to perform this task:
-
Create the following function in my script:
function deleteForever(userId, labelName) { var threads = GmailApp.search("in:trash label:" + labelName); for (var i = 0; i < threads.length; i++) { Gmail.Users.Messages.remove(userId, threads[i].getId()); } }
To enable advanced services for this script, locate
Resources
on the menu, and selectAdvanced Google services...
Enable
Gmail API
on the list.Before selecting
OK
, click on theGoogle Developers Console
link. Search forgmail
, and enable the service there as well.Done, select
OK
; the function should now work. (Comment: as mentioned in the link @karan provided, one can use"me"
for userID, or alternatively provide one's Gmail address:"<address>@gmail.com"
.)
(Steps to enable advanced services for my script are based on Google's guide here.)
It is not possible, by design, to delete an email permanently using GmailApp.
This script works for Google Apps Script. You have to just connect and auth services together.
function myFunction() {
var labelName = "deleteForever"
var threads = GmailApp.search("in:trash label:" + labelName);
for (var i = 0; i < threads.length; i++) {
Gmail.Users.Messages.remove('me', threads[i].getId());
}
}