Newbies FSC Posted December 11, 2019 Newbies Posted December 11, 2019 I have an email account I’m pulling unread messages from without issue but it has grown to have over 30,000 old messages so I’m trying to write a script to delete the read messages. The issue is that I obviously can’t pull all the read messages at once so I created a set of loops one inside the other, I fetch 200 messages then loop through them and flag them as deleted. Then I fetch another 200 and loop around to delete those. The first 200 get deleted but no more after that. Do I need to disconnect and reconnect before each new fetch?
ryan360Works Posted December 11, 2019 Posted December 11, 2019 Hello, You shouldn't need to disconnect and reconnect. I was able to set up a nested loop and delete messages in batches without an issue. Are you getting any errors? Perhaps your outer loop isn't getting to the second iteration? On a side note, is there an interface for this mail server? Either something like Apple Mail or web based client like Gmail? I ask because it may be faster and easier to use the interface to delete over a script.
Newbies FSC Posted December 11, 2019 Author Newbies Posted December 11, 2019 I don't get any errors when I step through using the debugger and if I let it run my logs show it completes but then I open up the web based mail client and the total number of messages has only gone down by whatever I set the initial fetch to. I was simply going in and deleting the old messages manually but this is an ongoing process and I wanted to add a delete function to the main script so after it has read the recent new messages it can delete some of the older messages and keep the overall mailbox size down to a reasonable level. The other reason I want to have the script clear the old mail is that I got lazy and let it grow until my available mailbox space got too low and now I've got over 20,000 messages to delete, a slow process when each page of the web client only shows 200 max. In the end I want to keep several months of messages in the mailbox and between 75 and 125 new messages come in daily so each day when the script runs it would be great to have it keeping the mailbox up to date which I suspect will be around 3000 messages. Here is the current script (after the EmailConnectIMAP script and followed by EmailDisconnect) Go to Layout [ “EmailSettings” (EmailSettings) ] Set Variable [ $ViewedMessages; Value:EmailGetMessageCount( "Viewed:True" ) ] Set Variable [ $MessageBufferCount; Value:EmailSettings::BufferMessageCount ] (set to 3000) Set Variable [ $MaxFetch; Value:EmailSettings::Max Fetch ] (set to 200) Set Variable [ $MessagesLeftToDelete; Value:$ViewedMessages - $MessageBufferCount ] Set Field [ EmailSettings::_result; EmailReadMessages( "progress=" & EmailSettings::Progress Bar; "mailbox=INBOX" ; "Max=" & $MaxFetch; "attachments=false" ; "viewed=true" ; "deleted=false" ) ] #pulled the following from Max= & EmailSettings::Max Fetch for testing Commit Records/Requests [ No dialog ] If [ EmailSettings::_result = "ERROR" ] Set Field [ EmailSettings::_dialog; "Could not read messages: " & EmailLastError ] Go to Layout [ “EmailLog” (EmailLog) ] New Record/Request Set Field [ EmailLog::Desc; "An error occurred: " & EmailLastError & "¶¶" & "Version: " & EmailVersion ] Halt Script End If #loop through messages pulling in groups of maxfetch Loop #Loop over the messages, mark each as deleted Loop Exit Loop If [ $$NewMessagesCount > $MaxFetch ] Set Variable [ $Result; Value:EmailGetNextMessage ] Set Variable [ $Result; Value:EmailMessageSetFlag("deleted") ] Set Variable [ $$NewMessagesCount; Value:$$NewMessagesCount + 1 ] End Loop Set Variable [ $MessagesDeleted; Value:$MessagesDeleted + $MaxFetch ] Set Variable [ $MessagesLeftToDelete; Value:$MessagesLeftToDelete - $MaxFetch ] Exit Loop If [ $MessagesLeftToDelete < 1 ] Set Field [ EmailSettings::_result; EmailReadMessages( "progress=" & EmailSettings::Progress Bar; "mailbox=INBOX" ; "Max=" & $MaxFetch; "attachments=false" ; "viewed=true" ; "deleted=false" ) ] End Loop Set Field [ EmailSettings::Skip; EmailSettings::Skip - $MessagesDeleted ] Go to Record/Request/Page [ First ] Commit Records/Requests [ No dialog ]
ryan360Works Posted December 12, 2019 Posted December 12, 2019 17 hours ago, FSC said: Set Variable [ $ViewedMessages; Value:EmailGetMessageCount( "Viewed:True" ) ] Is this a typo? If not that might be the issue as "Viewed:True" isn't valid and would be ignored by the plugin which may be throwing off your exit condition for the outside loop
Newbies FSC Posted December 13, 2019 Author Newbies Posted December 13, 2019 I will check that out but strangely that is working because I have checked the numbers, worst case it would come back as 0 and the routine would delete too many messages...
ryan360Works Posted December 13, 2019 Posted December 13, 2019 Another thing I just noticed is you are not passing in "readonly=false" in the EmailReadMessages call which should throw an error when you try to call EmailMessageSetFlag so I am surprised that the deletion even works the first iteration. Try adding that flag to the EmailReadMessages function calls and see if you get different results.
Recommended Posts
This topic is 1875 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now