I've found a solution.
Foreach mail i do something. If it's done i push to message number into an array.
After the foreach i remove the messages by reversing the values in the array en then delete them.
PHP Code:
foreach ($mail as $messageNum => $message) {
// do something
array_push($rm, $messageNum);
}
if (sizeof($rm) > 0) {
ZMailRemoveMessage($mail, $rm);
}
function ZMailRemoveMessage(Zend_Mail_Storage_Abstract $storage, $ids)
{
$ids = (array) $ids;
if (false === rsort($ids)) {
throw new Exception('huh?');
}
while (null !== $idx = array_shift($ids)) {
$storage->removeMessage($idx);
}
return $storage;
}