Script to cleanup mail directories

Discussion in 'Tips/Tricks/Mods' started by macguru, Aug 30, 2018.

  1. macguru

    macguru Member HowtoForge Supporter

    Hi !
    I use this script for casual cleaning of mail directories on Debian. It deletes all files but leaves directories intact. May be someone could find it useful.

    find /var/vmail/ -name '*' -type f -exec rm -v {} \;
  2. Jesse Norell

    Jesse Norell ISPConfig Developer Staff Member ISPConfig Developer

    As a caution to anyone who might try this and doesn't understand what it's doing, this is no selective cleanup of things, it will wipe out all email and configuration files for every email domain on your server - do NOT just casually run this!

    If you do, you can restore mailbox config like quota and ISPConfig mail filters by doing a resync in ISPConfig, but any email you previously had will be gone. If that is your intent, and you are running dovecot, a cleaner means of doing that is with doveadm, which will keep all your config files in tact.
  3. till

    till Super Moderator Staff Member ISPConfig Developer

    I second @Jesse Norell 's concerns, this can be really dangerous. You may use this if you really know what you're doing, but it will e.g. mess upa dovecot's quota calculation. So better not do a cleanup in that way :)
  4. macguru

    macguru Member HowtoForge Supporter

    Thanks for comments.

    Well, probably I should better describe purpose of this script. My ISPConfig setup runs under KVM / oVirt virtualization manager. After making backup copy I wipe out all messages in mail directories - they have no purpose anymore and only waste disk space. And if backup copy (with mail directories intact) get restored and connected back, there are tons of old duplicate messages flooding mailboxes.

    BTW, what is a shell command to resync mailboxes and mailfilters from ISPConfig DB? I'll add them to the script to save some time connecting via Web interface. Thanks.
  5. Jesse Norell

    Jesse Norell ISPConfig Developer Staff Member ISPConfig Developer

    There's no "command line" interface, if anything it would be via the api, and you'd have to write a script to call the resync; I don't know if that's even in the API though (@till would know off the top of his head).

    What you can do instead is just delete the email with doveadm, eg. try:
    doveadm expunge -A mailbox '*' all
    I don't have a system that's safe to test that on right now, but looks like that should be it. Make sure you have iterate_query set in /etc/dovecot/dovecot-sql.conf (which is the default in recent ISPConfig versions).

Share This Page