It's more likely that it's timing out while waiting for the imap server to complete the operation. Instead of sending a mass delete of a bunch of messages, break it up into chunks. Something along the lines of:
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...
}
Untested of course. Hopefully that gets you going in the right direction.