Search code examples
google-apps-scriptchatarchivegoogle-hangouts

Archive all chats in Google Hangouts


I'm looking to find / write a script to archive Google Chat messaged (hangouts). I thought that this would work, but it doesn't seem to do anything. Can anyone point out why its not working or what I've done wrong?

OR - If anyone else has a solution for what Im trying to do, that would be great too!

function ArchiveChats() {
var threads = GmailApp.getChatThreads(0, 100);
GmailApp.moveThreadsToInbox(threads);
GmailApp.moveThreadsToArchive(threads);
}

Solution

  • The problem with your code is that it gets the first 100 chat threads from anywhere in your mailbox rather than from your inbox.

    Here's a function that will archive all chat threads that are not already archived:

    function archiveAllChats() {
      var query = 'in:chats label:inbox';
      var MAX_THREADS = 500;
    
      var chatThreadsToArchive = GmailApp.search(query, 0, MAX_THREADS);                                         
      while (chatThreadsToArchive.length > 0) {
        Logger.log('Archiving %s chats', chatThreadsToArchive.length);
        GmailApp.moveThreadsToArchive(chatThreadsToArchive);
        chatThreadsToArchive = GmailApp.search(query, 0, MAX_THREADS);
      }
      Logger.log('All chats are archived');
    }