简体   繁体   English

尝试使用Python和Exchangelib删除收件箱外的文件夹中的多个电子邮件

[英]Attempting to delete multiple email messages in folder outside of inbox with Python and Exchangelib

I'm trying to delete all email messages in a folder outside of my account.inbox with Python 3 and exchangelib. 我正在尝试使用Python 3和exchangelib删除我的account.inbox之外的文件夹中的所有电子邮件。

testFolder = account.root / 'Top of Information Store' / 'Test'    
emails = testFolder.all().order_by('-datetime_received')
for email in emails:
    email.delete()
    # FWIW
    #print(email.subject)
    #email.move(to_another_folder)
    #both work fine.

The above only prints all emails and their properties 以上仅打印所有电子邮件及其属性

I have also tried: 我也尝试过:

items=[]
testFolder = account.root / 'Top of Information Store' / 'Test'    
emails = testFolder.all().order_by('-datetime_received')
for email in emails:
    items.append(Message(folder=testFolder))
items.save()
items.delete()

The above hangs and gives a memory error: 上面挂起并给出内存错误:

File "C:\Python36-32\lib\site-packages\exchangelib\services.py", line 89, in _
get_elements
    response = self._get_response_xml(payload=payload)
  File "C:\Python36-32\lib\site-packages\exchangelib\services.py", line 171, in
_get_response_xml
    res = self._get_soap_payload(response=r, **parse_opts)
  File "C:\Python36-32\lib\site-packages\exchangelib\services.py", line 260, in
_get_soap_payload
    root = to_xml(response.iter_content())
  File "C:\Python36-32\lib\site-packages\exchangelib\util.py", line 365, in to_x
ml
    return parse(stream, parser=forgiving_parser)
  File "C:\Python36-32\lib\site-packages\defusedxml\lxml.py", line 134, in parse
  elementtree = _etree.parse(source, parser, base_url=base_url)
  File "src\lxml\etree.pyx", line 3424, in lxml.etree.parse
  File "src\lxml\parser.pxi", line 1857, in lxml.etree._parseDocument
  File "C:\Python36-32\lib\site-packages\exchangelib\util.py", line 340, in getv
alue
    res = b''.join(self._bytes_generator)
MemoryError

I'm not sure what else I should try, especially since the former works to move and print emails just fine. 我不确定我还应该尝试什么,特别是因为前者可以移动和打印电子邮件。

There's no need to collect the items first. 没有必要先收集物品。 This will consume a lot of memory, as you found out. 正如您所发现的,这将消耗大量内存。 Instead, you can call .delete() directly on the QuerySet. 相反,您可以直接在QuerySet上调用.delete() This will fetch only the item IDs and delete them in batches. 这将仅获取项目ID并批量删除它们。

There is also an .empty() method which results in a call to the EmptyFolder service. 还有一个.empty()方法,它会调用EmptyFolder服务。 This will delete all items in the folder without even fetching the IDs: 这将删除文件夹中的所有项目,甚至不会获取ID:

testFolder.all().delete()
# Or even faster:
testFolder.empty()

Maybe instead of delete try move to trash? 也许不是删除尝试移动到垃圾桶? or soft delete? 还是软删除?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM