Jump to content
Sign in to follow this  
FSC

Deleting old messages

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 ]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.