I used a tool called #archivemail for -- guess what -- archiving and deleting emails from an #IMAP server older than a given number of days.
The archived emails were stored in compressed #mbox format and were easily searchable using #mairix and #mutt.
Unfortunately, archivemail is no longer maintained, and there are no packages for current #debian and #ubuntu distributions.
#mairix #ubuntu #debian #mutt #mbox #imap #archivemail