my $msgset = Mail::IMAPClient::MessageSet->new( $imap->messages ); my $msg_ids = []; if ( $msgset ) { $msg_ids = $msgset->unfold; } while ( my @cur_block = splice @$msg_ids, 0, 500 ) { # delete the current block of messages... }