Background Users tend to press the 'delete' button in Gmail (or in Outlook via google apps sync), but we want to archive all messages (google clears trash every 30 days).
Solution so far The following script runs on a trigger every hour, moving any threads in trash into the archive.
function archiveTrash() {
var threads = GmailApp.getTrashThreads(0, 100);
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
Problem Users are reporting that messages are disappearing from their inboxes.
Probable cause I believe that this is what is happening: the user is dealing with a thread of messages and deletes some messages in the thread (users generally use outlook, so they see each message individually). However other messages in the thread remain in the user's inbox. Then getTrashThreads is picking up threads which contain at least one deleted message, and the script is moving the whole thread to the archive, including the messages which were in the inbox.
Solution? This is where I am stuck. How do I archive only trashed messages, not the whole thread? Is there a way to stop gmail grouping messages into threads at all? Am I even right about what is going on?