1

I'm very new to programming so I'm looking for some help here. I have an email folder that has over 200k emails. I am trying to delete them all because they're slowing me down. I wrote a script in Python 3.7.2 using IMAPClient to try and delete them en masse but I keep getting an error.

I have tried to delete all emails in the folder, and I have tried to delete a single days worth. An average single day has about 2k emails. Using both 'ALL' and 'ON DD-MMM-YYYY"

import imapclient
imapObj = imapclient.IMAPClient('imap.server.com', ssl=True)
imapObj.login('email', 'password')

import pprint
pprint.pprint(imapObj.list_folders())

[((b'\\NoInferiors',), b'/', 'INBOX'),
 ((b'\\HasNoChildren',), b'/', 'Folder I want to delete'),
--all folders--

imapObj.select_folder('Folder I want to delete', readonly=False)
{b'PERMANENTFLAGS': (b'\\Answered', b'\\Flagged', b'\\Draft',         
b'\\Deleted', b'\\Seen', b'$Forwarded', b'$NotJunk', b'NotJunk', 
b'$MailFlagBit1', b'$MailFlagBit0', b'$MailFlagBit2', b'\\*'), 
b'FLAGS': (b'\\Answered', b'\\Flagged', b'\\Draft', b'\\Deleted', 
b'\\Seen', b'$Forwarded', b'$NotJunk', b'NotJunk', b'$MailFlagBit1', 
b'$MailFlagBit0', b'$MailFlagBit2'), b'EXISTS': 3792507, b'RECENT': 
0, b'UNSEEN': [b'3792504'], b'UIDVALIDITY': 1398179813, b'UIDNEXT': 
7577201, b'READ-WRITE': True}

UIDs = imapObj.search('ALL')
imapObj.delete_messages(UIDs)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    imapObj.delete_messages(UIDs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
   packages/imapclient/imapclient.py", line 1171, in delete_messages
   return self.add_flags(messages, DELETED, silent=silent)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
   packages/imapclient/imapclient.py", line 1081, in add_flags
   return self._store(b'+FLAGS', messages, flags, b'FLAGS', 
   silent=silent)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
   packages/imapclient/imapclient.py", line 1567, in _store
   uid=True)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- 
   packages/imapclient/imapclient.py", line 1535, in 
   _command_and_check
   typ, data = self._imap.uid(command, *args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib 
   .py", line 879, in uid
   typ, dat = self._simple_command(name, command, *args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib 
   .py", line 1196, in _simple_command
   return self._command_complete(name, self._command(name, *args))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib 
   .py", line 1027, in _command_complete
   raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: UID command error: BAD [b'Command line too large']

I expect the emails to be delete but the command just errors out to the above.

golftech
  • 11
  • 1

1 Answers1

1

The list of UIds is to long to make one request , we need to split every deletion.

UIDs = imapObj.search('ALL')
for UID in UIDs
    imapObj.delete_messages([UID])
    print( 'Deleted--->'+str(UID))
  • Can I do this in the shell? Or should I put this into a .py and run the module? Will it continue to delete until there are no messages and then give an error? I don't want it to delete anything else. – golftech Jan 14 '19 at 17:51
  • You put this in the .py. It will delete all the message in list of UIDs, you have to change the search option 'ALL' to be sure 100 % that there is no problem – Tarik Elkalai Jan 14 '19 at 17:55
  • I used delete_folder to delete it and it worked. I'm going to try your way on another folder to practice as well. Thanks again! – golftech Jan 14 '19 at 18:24