Skip to content

Instantly share code, notes, and snippets.

@spectra
Last active August 29, 2015 14:03
Show Gist options
  • Save spectra/582ac34e9eac4def5762 to your computer and use it in GitHub Desktop.
Save spectra/582ac34e9eac4def5762 to your computer and use it in GitHub Desktop.
Gmail All Mail periodic deletion
#!/usr/bin/env ruby
# Delete files from Gmail All Mail folder
# Needs proper Gmail account configuration
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (commit 34973274ccef6ab4dfaaf86599792fa9c3fe4689):
# <pablo@propus.com.br> 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='user@example.com'
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