Last active
August 29, 2015 14:03
-
-
Save spectra/582ac34e9eac4def5762 to your computer and use it in GitHub Desktop.
Gmail All Mail periodic deletion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Delete files from Gmail All Mail folder | |
# Needs proper Gmail account configuration | |
# | |
# ---------------------------------------------------------------------------- | |
# "THE BEER-WARE LICENSE" (commit 34973274ccef6ab4dfaaf86599792fa9c3fe4689): | |
# <[email protected]> wrote this file. As long as you retain this notice you | |
# can do whatever you want with this stuff. If we meet some day, and you think | |
# this stuff is worth it, you can buy me a beer in return Pablo Lorenzzoni | |
# ---------------------------------------------------------------------------- | |
require 'net/imap' | |
require 'date' | |
USERNAME='[email protected]' | |
PASSWORD='hackme' | |
MAIL_SERVER='imap.gmail.com' | |
SOURCE_MAILBOX='[Gmail]/All Mail' | |
imap = Net::IMAP.new(MAIL_SERVER, 993, usessl = true, certs = nil, verify = false) | |
imap.login(USERNAME, PASSWORD) | |
imap.select(SOURCE_MAILBOX) | |
date = (Time.now - (6 * 30 * 24 * 60 * 60)).to_datetime # 6 months ago | |
to_delete_ary = imap.search(["BEFORE", Net::IMAP.format_date(date)]) | |
puts "#{to_delete_ary.length} messages to delete since #{date}" | |
imap.store(to_delete_ary, "+FLAGS", [:Deleted]) | |
imap.expunge | |
puts "done" | |
imap.logout | |
imap.disconnect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment