Archive Gmail messages to Google Drive folders

By using this script you can archive Gmail messages to Google Drive including message attachments. First of all create a copy of the following spreadsheet document: Archive Gmail messages to Google Drive

The spreadsheet document should contain a new menu item called “Gmail Archiver”. Click on “Gmail Archiver” menu and select “Initialize”. You should see an authorization request like the following:

Archive Gmail messages to Google Drive 1


Press “OK” button to authorize and follow the authorization process. Now click again  on “Gmail Archiver” menu and select “Initialize”. You should see now a confirmation box about the successful initialization of the script.

Archive Gmail messages to Google Drive 2

The script created for you a new Gmail label called “Archive to Drive” and a new drive folder called “Email Archive” where Gmail messages will be stored.

Now go to Gmail and add the Gmail label “Archive to Drive” to each message you want to archive:

Archive Gmail messages to Google Drive 3

Now go back to the spreadsheet document and click  on “Gmail Archiver” menu and select “Archive Gmail Messages”. After some seconds you should see a message like:

Archive Gmail messages to Google Drive 4


Now go to Google Drive inside folder “Email Archive” and you should see a new folder representing the exported email message:

Archive Gmail messages to Google Drive 5


Now entering the newly created folder you should see the email messages content saves as a PDF file and all message attachments saved as separate files:

Archive Gmail messages to Google Drive 6

Enjoy and happy scripting!

Consultant, Lean Thinker, Agilist, Technology Lover. Dream: Being worth a TED talk. Project Manager in a wide variety of business applications. Particularly interested in innovation projects, as well as close interaction with costumers.

Tagged with: , ,
Posted in Gmail, Google Drive
73 comments on “Archive Gmail messages to Google Drive folders
  1. Richard says:

    This is a great idea. I have been looking for a way to archive certain labeled gmail folders…your definitely on to something! For my needs I need to filter and apply formulas to the contents of the archived gmail emails once they have been archived into the spreadsheet. Is it possible to do this using your script?

    • Hello Richard,
      actually this script doesn’t use Spreadsheet as a bridge to archive Gmail messages. They are directly stored inside Google Drive folders. An intermediate step could be added to record archived messages on a Spreadsheet document and then apply needed calculations and formulas there. Right now this is not implemented since this was mainly a base where to build on.

  2. Luciano says:

    Is it possible to store the the messages as native Google Drive Documents? Doing so would not count to the Drive storage I think..

    • Hello Luciano, I don’t think so, since Google changed the Documents format from HTML to another proprietary format. Anyway this is a good point I’ll will investigate about this possibility.

      • Excelent job. I’ve expecting this solution for while and I´d like to ask how hard it’d be to “filter” specifically email keys (as in Gmail filter) to archive into a specifically GDocs folder?

  3. tasman says:

    Thanks Robert works like a charm!

    Here an additional usage tip, as not everybody seems to be aware of this.

    If you want to move emails with large attachments you can use “size:” as a search criteria in gmail, like so:


    This will list all emails equal or larger then 25MB, note the size given should be in bytes (MB x 1024 x 1024).
    Then apply the “Archive to Drive” label, go back to the spreadsheet and apply “Gmail archiver => Archive gmail messages.”

  4. tasman says:


    My apologies to Marcello, I daftly echoed “thanks Robert” from a pingback whereas the thanks should go to Marcello Scacchetti.

  5. Christy says:

    “Gmail Archiver” does not show up as a new menu item on the spreaddsheet document ARCHIVE GMAIL MESSAGES TO GOOGLE DRIVE.


  6. John says:

    Ran script after setting up archiver.

    Google indicates I’ve run the script too many times today. Ran it once.

    Am I doing something wrong?

  7. Hi Marcello

    The new menu item “Gmail Archiver” does not appear for me either. Any suggestions?

    Many thanks


  8. Joshua Silverman says:

    First, this is brilliant and a lifesaver. Thank you so much for sharing this.

    One question, is it possible to change the naming convention to start with Year/Month/Day instead of Month/day/Year so they can archive can be sorted chronologically?

    • Hello Joshua,
      yes it’s definitely possible, but you have to edit the script. You have to change line 45 from:
      var messageDate = Utilities.formatDate(messagesArr[k].getDate(), Session.getTimeZone(), “dd/MM/yyyy – HH:mm:ss”);
      var messageDate = Utilities.formatDate(messagesArr[k].getDate(), Session.getTimeZone(), “yyyy/MM/dd – HH:mm:ss”);

  9. Steve Frank says:

    I love what this is doing but is there any way to only get attachments? I get 100 emails a day where I have to individually open attachments and print them. Looking for a workaround.

  10. Russ says:

    It works great. However, it timed out after a pretty short amount of time. Is that a problem with the script or something on Google’s end? If it has a short run length it actually won’t be terribly useful for backing up emails considering I have a ton of emails to archive (and I’m sure I’m not alone!).

  11. Jarrah says:

    Hi there,
    This works great in gmail accounts, however I run google apps on my domain name and my email although it is not I can access it through the gmail website..
    So it really is gmail.
    When I try to do this, the gmail archive menue does not appear.
    Any workaround???
    Great idea by the way

  12. This is a great tool. a lot of my customers will be happy to see this. Thanks for writing this.

  13. I love the concept of the script, however I am having some issues.

    I’ve followed the instructions to the letter, but when I attempt to run the script, I get the error: “Authorization is required to perform that action. Please run the script again to authorize it.”

    I’ve attempted many combinations of choosing “initialize” and refreshing, trying different browsers on different systems, and trying different accounts, no dice.

    I have 2-step verification turned on all the accounts if that makes a difference. Would love some assistance. If we can get it working reliably, it would make my day. Thanks!


  14. Fabien Cordiez says:

    Google docs do not take up any space in Google Drive! I am keen to find an add-on or a Script which allows for gmail messages and attachments to be saved into Google Drive in ‘Google doc’ format. Any ideas whether such a script exists?

  15. Ds says:

    Hi I am getting authorisation issues an no label created in Gmail when I run both scripts ? Any ideas ? Thank you very much,

  16. I think I found the solution to my own question. I was testing on my personal Gmail account. When I tested on my Google Apps account it appears to be working great. Thanks!

  17. Philip says:

    Hey Marcello! Great script!
    Can I kindly ask you to if it is possible to add all names of attached lables from gmail to the folder in drive?
    I have checked the code a bit – but me being a beginner it is to hard for me to change anything..

    Cheers, Philip

    • Philip, thanks :) Yes, adding labels to the directory created inside Google Drive it’s possible but requires some work on the script, it’s not achievable by a quick hack. If you are really interested in that feature just contact me here

  18. The date format of the saved file is dd-mm-yyyy and this is not sort-order-friendly, both into drive and into local file systems, it should be just yyyymmdd.

  19. What about putting the script, once properly initialized, amongst those scripts triggered every 15 minutes into google drive? This way you wouldn’t have to open the spreadsheet every time. Just an idea.

    • Hello Tiziano,
      you can set the trigger configuration manually from the Google Apps Script editor under the menu item: Resources->Current project’s triggers… so you can adapt it to your specific time sync needs.

      • Thank you. I am posting a comment today I tried to post but failed some days ago.

        • Thank you Marcello, this is pure gold.

          I am writing in English for other’s commenters sake.

          I run the script and, even if I tagged just 3 messaged, the whole threads where those messages were into were saved. Now, I do not know if this is a gmail feature or the script’s, I disabled threaded view into gmail. I guess that since the script is mainly for attachment that being able to focus on single messages would be much better than saving the whole thread (which is cluttering).

  20. Matt says:

    This is not working for me. It says, ‘Service invoked too many times for one day: docslist create.’

    How do I fix it?

    • Hello Matt, Google imposed over Google Apps Script some quota limitations. If you are trying to archive too many messages a day or in a single run you will get that message. I can suggest to execute the archival procedure in small batches. Hope this helps.

  21. Katharine says:

    I successfully used the program to archive my messages. However, it still appears that they are in my gmail account taking up room. When the program runs the messages move out of the archive to drive folder. Is there any way to automatically delete the archived mail from my gmail or keep them in the archive to drive folder so that it is easier for me to delete them?

  22. Ashlee says:

    Is it possible to share the email archive folder and have the script running on several gmail accounts and archiving to the shared folder? I ran a few test runs to different accounts and it didn’t appear to be able to work, however, maybe I am not going about it in the right way.

  23. I haven’t used this myself (yet) because I’m not near the limit. I do, however, definitely see the advantage of this tool.

    One suggestion for a common request here: When you run the portion of the tool that marks the emails for Archive to Drive, go to your Gmail account and select on that label. Then add a 2nd label to those emails (any unique label that is not used by the script or any other emails). After the Archive to Drive is complete, you can then go back to Gmail and select all emails from that label and delete them. That will complete the cycle and remove them from your storage requirements in Gmail. This is not done in the script, I presume, for the very solid reason that they do not wish to monkey around with deleting anyone’s precious emails. You confirm that all have been properly archived, then you control the delete function.

    Value resource – Thank you Marcello. I’ll be back here.

  24. Ryan says:

    It works for a while and then I start getting an error that the Drive folder “Email Archive” cannot be found. But the folder is there.

  25. prasong says:

    i can not preview images/video in google drive folder via this scripts.but i can view images/video if upload directly from my PC.
    please advise,thank you for your great scripts.

  26. Swifty says:

    Works like a charm.
    I think Google should pay you for the good work you have done and have them add it straight into Gmail. (Minus the spread sheet of course)

    Thanks very much!

  27. Jeff Ober says:

    Can the script be modified to save the emails into a folder structure that matches my label structure? In other words, if I have a label with three nested labels, and I use the script to archive all messages, can it be saved in Drive as a folder with three sub-folders?

  28. Peter says:

    Great job on the script. A couple of questions.
    1) I am getting the error message “Deserialization of continuation failed” at the end of the scrip run although all seems to work ok. Is this an issue?
    2) I realise that the “Archive to Drive” label is removed during the archive process but is it possible to easily add another label such as “Archived”?

    Thanks again.

  29. Jason Morris says:

    Thanks for this!

    I have published a (probably poorly) hacked version that uses the first three columns of the spreadsheet to get the source label, the destination drive folder, and the destination label.

    The published version is here. No promises, but you might be able to make it work for you.

  30. Bensin says:

    Everything time i go to intialize i get the same popupytime i try
    It’s like it’s resetting that setting everytime i try to intialize.

  31. Gino says:

    You consider the introduction of software purchase of services to assist the user, G-mail transfer to the driver.
    Use the script in Google will be limited, and I can not store large amounts of mail at once.

  32. Loren Nason says:

    This is pretty sweet

    Is there a way or line to add that also grabs the headers?


  33. Roger says:

    thank you very much.

  34. Tee says:

    THANK YOU!!!!!!!!!!!!!!!!!!!!!!!

  35. rob al says:

    Mario – i now get this message from time to time:
    Service invoked too many times in a short time: driveWriteVolume rateMax. Try Utilities.sleep(1000) between calls

    Do you get this too?

  36. sunil says:

    folder in google drive is created as also the folder in gmail, but the folder in google drive is empty ! the mail does not get transferred to the google drive. what could be the reason ?

  37. Amnon says:

    Hi Marcello,

    Your script provides a very essential service. Thank you for making it available!

    I’ve tried it with an email with pictures attached in jpg format. Unfortunately it created corrupted image and produced the error message “Unexpected exception upon serializing continuation”. Have I done anything wrong?

    Many thanks, Amnon

  38. Bill says:

    Great Script!! Works like a charm!

  39. Qiang Song says:

    Thanks a lot for this great tool.

    I modified your scripts to better fit my needs. First, instead of creating a folder for each message, my script creates a folder for each sender; second, my script does not archive email messages, only the attached files.

    I put my script on github with ackowledgement for your original contribution. Hopeful you are happy with that:

    thanks again!

  40. Badi says:

    Works well. Thank you :)

    I have noticed that the name on some e-mails are very long in drive. If I try to open in Windows, it says the name is too long and it won’t open the PDF. Of course I can manually shorten the name but if there is a workaround for this let me know.

  41. Kate says:

    My attachments did not come over as PDF but rather as script. Help?!

  42. Celine says:

    Just want to say: this is awesome!!! Thanks a lot!
    Notes: click the link, go to ‘File’ to ‘make a copy’, then you can see “Gmail Archiver” in the new file!

  43. Claire says:

    I got a an error message ‘exceeded maximum execution time’.

    What does that exactly mean?

  44. Dawn says:


    The script doesn’t seem to be working at this time. There is no new menu item called “Gmail Archiver”. Is this tool still available? Thank you.


2 Pings/Trackbacks for "Archive Gmail messages to Google Drive folders"
  1. […] why I’m thrilled to have found this: a script that archives old Gmails to Google Drive. (Thanks, Robert.) It’s pretty amazing, actually. I tried it and it works. The script was […]

  2. […] Archive Gmail messages to Google Drive folders [Jellybend via TechCrunch] […]

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>