WebFaction
Community site: login faq

Is there any way I can delete emails in a mailbox, which are over x days old? I need this in order to control the disk usage of a few client mailboxes which are using POP. Any help would be nice. Thanks!

asked 21 Feb '14, 17:16

Roger%20Mac's gravatar image

Roger Mac
112
accept rate: 0%


Most POP email clients can do this for you via their POP account settings. The setting will be labeled something like "Delete mail that is older than..." or "Keep mail on server no longer than...".

If you prefer to do this outside of the POP client settings, you can use a script similar to what we have in our documentation at "Automatically Emptying a Folder" - just change the IMAP search criteria from 'ALL' to whatever IMAP date criteria you need.

I think the modified script would look something like this:

#!/usr/local/bin/python2.7
import imaplib
from datetime import timedelta, date

keep_days = timedelta(days=14)
cutoff_date = datetime.date.today() - keep_days

M = imaplib.IMAP4('mail.webfaction.com')
M.login('<mailbox_name>', '<mailbox_password>')
M.select('<folder>')
typ, data = M.search(None, '(BEFORE "%s")' % (cutoff_date.strftime('%b-%d-%Y'),))
for msgid in data[0].split():
    M.store(msgid, '+FLAGS', '(\\Deleted)')
M.expunge()
M.close()
M.logout()

Hope that helps!

permanent link

answered 21 Feb '14, 19:54

seanf's gravatar image

seanf ♦♦
12.0k21534
accept rate: 37%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×14
×9
×1

question asked: 21 Feb '14, 17:16

question was seen: 1,077 times

last updated: 21 Feb '14, 19:54

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2017 PARAGON INTERNET GROUP LIMITED
WEBFACTION IS A SERVICE OF PARAGON INTERNET GROUP LIMITED
REGISTERED IN ENGLAND AND WALES 7573953 - VAT REGISTRATION NUMBER 182147021
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM