Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Deleting old messages

Featured Replies

  • Newbies

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?

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.

  • Author
  • Newbies

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

  • Author
  • Newbies

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

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.

Create an account or sign in to comment

Important Information

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

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.