我编写了一个脚本来删除标签中超过10天前的所有电子邮件。当我运行脚本时,每次删除一些电子邮件,但不是所有电子邮件都删除。我有大约7个标签,其中许多有~2k的电子邮件,最大的有大约18k。
function empty_mail() {
// get GmailLabels as variables
var labelOne = GmailApp.getUserLabelByName("myLabel1");
var labelTwo = GmailApp.getUserLabelByName("myLabel2");
// some more labels ...
var lastLabel = GmailApp.getUserLabelByName("myLastLabel");
// array of GmailLabels (edited)
var labels = [myLabel1, myLabel2, myLastLabel];
var days_old = 10; // all messages older than 10 days will be deleted in these labels
// set threshold date in past
var threshold_date = new Date();
threshold_date.setDate(threshold_date.getDate() - days_old);
// format threshold date as string and log
var mnth = threshold_date.getMonth() + 1;
var dy = threshold_date.getDate();
var yr = threshold_date.getFullYear();
var strDate = mnth + "/" + dy + "/" + yr;
Logger.log("Mail older than " + strDate + " will be deleted.");
// run delete_mail function on each label
for (var i=0; i < labels.length; i++){
Logger.log("==== Deleting old mail in: " + labels[i].getName() + " ====");
delete_mail(labels[i], threshold_date);
}
}
function delete_mail(theLabel, oldDate){
var count = 0;
var threads = theLabel.getThreads();
// if message is older than threshold date, move it to trash
for (var i = 0; i < threads.length; i++){
if (threads[i].getLastMessageDate() < oldDate){
threads[i].moveToTrash();
count++;
}
}
Logger.log(" - deleted " + count + " messages from " + theLabel.getName());
}
乍一看和一些早期的测试,这很有效。但是当我注意到并非所有标签都被清除时,我开始记录并且记录器正在输出这样的东西:
[15-06-30 15:38:56:487 PDT] Mail older than 6/20/2015 will be deleted.
[15-06-30 15:38:56:488 PDT] ==== Deleting old mail in: <label1> ====
[15-06-30 15:38:56:921 PDT] - deleted 0 messages from <label1>
[15-06-30 15:38:56:921 PDT] ==== Deleting old mail in: <label2> ====
[15-06-30 15:38:57:626 PDT] - deleted 0 messages from <label2>
[15-06-30 15:38:57:627 PDT] ==== Deleting old mail in: <label3> ====
[15-06-30 15:38:57:900 PDT] - deleted 0 messages from <label3>
[15-06-30 15:38:57:901 PDT] ==== Deleting old mail in: <label4> ====
[15-06-30 15:39:02:593 PDT] - deleted 0 messages from <label4>
[15-06-30 15:39:02:594 PDT] ==== Deleting old mail in: <label5> ====
[15-06-30 15:39:02:849 PDT] - deleted 0 messages from <label5>
[15-06-30 15:39:02:850 PDT] ==== Deleting old mail in: <label6> ====
[15-06-30 15:39:30:078 PDT] - deleted 24 messages from <label6>
[15-06-30 15:39:30:079 PDT] ==== Deleting old mail in: <label7> ====
[15-06-30 15:39:36:633 PDT] - deleted 0 messages from <label7>
问题在于,至少在标签4,6和7中,仍有许多(数百或数千)电子邮件仍应被删除。任何人都可以看到我的代码中的错误,是否有某种超时命中,或有其他人遇到过这样的事情?谢谢!
答案 0 :(得分:0)
我通过对代码进行更多调查解决了这个问题。在gmail脚本documentation for the getThreads() function中,它有这一行:&#34; 当所有线程的大小太大而系统无法处理时,此调用将失败。&#34;
在我的情况下,事实证明呼叫没有失败,但只返回前500个线程。为了解决这个问题,我使用了getThreads(start,max)调用,如下所示。
function delete_mail(theLabel, oldDate){
var threadStart = 0;
var threadMax = 100;
var totalDeletedCount = 0;
var totalThreadCount = 0;
var pageDeleteCount = 0;
// get 100 threads at a time
do {
var threads = theLabel.getThreads(threadStart, threadMax);
for (var i = 0; i < threads.length; i++){
if (threads[i].getLastMessageDate() < oldDate){
threads[i].moveToTrash();
totalDeletedCount++;
pageDeleteCount++;
}
}
threadStart += threadMax; //start at next max
threadStart -= pageDeleteCount; //subtract threads deleted in loop
pageDeleteCount = 0;
totalThreadCount += threads.length;
} while (threads.length == threadMax);
// log using totalThreadCount and totalDeletedCount
}
一次获得100个线程,然后通过添加100移动到下一个线程,然后减去许多线程,只是删除了。
其他问题:
可能的修复: