Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:40:54+00:00 2026-06-12T15:40:54+00:00

The goal is to remove bunch of email messages using imaplib. Email folder receives

  • 0

The goal is to remove bunch of email messages using imaplib. Email folder receives approximately 300k new messages a month. Only messages that are older than 1 month should be deleted. If executing this script it will delete old messages, but deletion takes a lot of time and simple for iteration does not look effective. It takes several hours.
By trying to increase speed with multiprocessing gives error.

What can you advise to improve the speed of deleting big amount of messages?

import sys
import datetime
from imaplib import IMAP4

# get the date a month from the current
monthbefore = (datetime.date.today() - datetime.timedelta(365/12)).strftime("%d-%b-%Y")

m = IMAP4('mail.domain.com')
m.login('user@domain.com', 'password')

# shows how many messages in selected folder
print m.select('Folder')
typ, data = m.select('Folder')

# find old messages
typ, data = m.search(None, '(BEFORE %s)' % (monthbefore))

# delete them
print "Will be removed:\t", data[0].split()[-1],"messages"
for num in data[0].split():
  m.store(num, '+FLAGS', '\\Deleted')
  sys.stderr.write('\rRemoving message:\t %s' % num)

# now expunge marked for deletion messages, close connection and exit
print "\nGet ready for expunge"
m.expunge()
print "Expunged! Quiting."
m.close()
m.logout()

Update: Rewrited part of a code, here is a 1000 times faster working variant (my server supports store command to more than 1000 messages at a time):

    def chunks(l, n):
        # yields successive n-sized chunks from l.
        for i in xrange(0, len(l), n):
            yield l[i:i+n]

    mcount = data[0].split()[-1]
    print "Will be removed", mcount, "messages"
    for i in list(chunks(data[0].split(), 1000)):
        m.store(",".join(i), '+FLAGS', '\\Deleted')
        sys.stderr.write('\rdone {0:.2f}%'.format((int(i[-1])/int(mcount)*100)))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:40:55+00:00Added an answer on June 12, 2026 at 3:40 pm

    I think the main problem here is that you’re calling STORE for each message. Each one of those round trips to the server takes time and when you’re doing lots of deletions this really adds up.

    To avoid all those calls to STORE trying calling it with multiple message ids. You can either pass a comma separate listed (e.g. "1,2,3,4"), ranges of message ids (e.g. "1:10") or a combination of both (e.g. "1,2,5,1:10"). Note that most servers seem to have a limit on the number of message ids allowed per call so you’ll probably still need to chunk the ids into blocks (of say 200 messages) and call STORE multiple times. This will still be much, much faster than calling STORE per message.

    For further reference, see the STORE Command section of RFC 3501. It shows an example of a STORE command taking a range of message ids.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, my goal is to remove everything inside ()'s except for strings that are
My goal is to convert list into array using split method then remove element(depends)
My goal is to remove home from any actions in that controller (see bold).
im using this code $output = preg_replace('/[^(\x20-\x7F)]*/','', $output); my goal is to remove this
My goal is to remove from the raw stack the records that are not
This is the goal: to remove Navigation Bar in some of the editors (no
My goal is to remove IE conditional comments from my documents' <head> , but
Goal is to make a dialog that appears on menu_key pressed, but it keeps
Goal: Produce an Excel document with information from 3 associated models that is similar
Goal: Gain datatype date with year and month Problem: Need to help to convert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.