
ipurge Command in Linux
The ipurge command in Linux deletes mail from the IMAP mailbox or partition based on age or size. An IMAP mailbox is an email storage location used with the Internet Message Access Protocol (IMAP), which allows email clients to access and manage messages on a remote mail server.
The ipurge command is a tool for mail server administrators to clean up mailboxes and remove older or larger emails in a controlled way. This tool can also be used in scripts to automatically maintain IMAP mailboxes by purging unnecessary messages at regular intervals.
Table of Contents
Here is a comprehensive guide to the options available with the ipurge command −
- Prerequisites for ipurge Command
- Syntax of ipurge Command
- ipurge Command Options
- Examples of ipurge Command in Linux
Prerequisites for ipurge Command
The ipurge command is a part of Cyrus, which is a widely-used mail server software that implements the Internet Message Access Protocol (IMAP). To use ipurge ensure Cyrus is installed.
To install Cyrus on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the following command −
sudo apt-get install cyrus-common
To install Cyrus on CentOS, use the following command −
sudo yum install cyrus-imapd
To install it on Fedora, use −
sudo dnf install cyrus-imapd
The ipurge command does not install to the system's default PATH. To find the path of the ipurge command's binary, use the find command −
sudo find / -name ipurge

To add this path to the system's PATH, use the following command −
echo 'export PATH=$PATH:/usr/lib/cyrus/bin' >> ~/.bashrc
The above command is appending the path to the .bashrc file. Now, source the file.
source ~/.bashrc
Now, the binary of the ipurge command is searchable.
which ipurge

Note that without performing the above configuration steps, the ipurge command binary will not appear using which command.
Syntax of ipurge Command
The syntax of the Linux ipurge command is as follows −
ipurge [options] [mailbox]
The [options] field is used to specify the various options to change command behavior. The [mailbox] field is used to specify the mailbox from which the messages are required to be purged.
ipurge Command Options
The options of the ipurge command are listed below −
Flags | Options | Description |
---|---|---|
-C config-file | Use a specified configuration file instead of the default imapd.conf | |
-f | --include-user-mailboxes | Force examination of mailboxes under INBOX.* and user.* |
-d days | --days=days | Specify the age of messages in days |
-b bytes | --bytes=bytes | Specify the size of messages in bytes |
-k Kbytes | --kbytes=Kbytes | Specify size in kilobytes (2^10 bytes) |
-m Mbytes | --mbytes=Mbytes | Specify size in megabytes (2^20 bytes) |
-x | --exact-match | Perform an exact match on age or size |
-X | --delivery-time | Use delivery time for date matches instead of the Date: header |
-i | --invert-match | Invert match logic for age/size (not equal, newer, smaller) |
-s | --skip-flagged | Skip messages with the \Flagged flag |
-o | --only-deleted | Purge only messages marked with the \Deleted flag |
-n | --dry-run | Display messages that would be deleted without actually deleting them |
-v | --verbose | Enable verbose output/logging |
Examples of ipurge Command in Linux
This section shows the usage of the ipurge command in Linux with examples. To use this command requires superuser privileges.
Purging Messages Larger than a Specified Size
To purge messages larger than the specified size, use the -b or --bytes option with the ipurge command −
sudo ipurge -b 50000 /var/spool/cyrus/mail/user/username
The above command will delete messages above 50 KB in size.
The /var/spool/cyrus directory is the partition where Cyrus stores mail. Replace /user/username with the starting letter of the user and the username of the account whose messages need to be purged.
Purging Messages Exactly Matching the Specified Size
To purge messages exactly matching the specified size, use the -x or exact-match option with the -b or --bytes −
sudo ipurge -x -b 50000 /var/spool/cyrus/mail/user/username
The above command will delete messages exactly equal to 50 KB.
Purging Messages Older than a Specified Number of Days
To purge messages older than the specified number of days, use the -d or --days option with the ipurge command. For example, to purge user's messages older than 10 days.
sudo ipurge -d 10 /var/spool/cyrus/mail/user/username
Using a Specific Configuration File
By default, the ipurge command uses the /etc/imapd.conf file for its configuration. However, to specify a different file, use the -C flag with the path of the configuration file.
sudo ipurge -C /home/user/ipurge.conf /var/spool/cyrus/mail/user/username
Dry Running
To dry run the ipurge command is a handy way to view the messages before deleting them. The dry-run will only display those messages that will be deleted according to the specified criteria. For dry-run, use the -n or --dry-run option with the ipurge command.
sudo ipurge -n -d 10 /var/spool/cyrus/mail/user/username
Inverting the Purge Criteria
To invert the specified criteria to purge, use the -i or --invert option with the criteria. The specified date will be for newer messages instead of older ones, and the size will be for smaller instead of larger ones. For example, the following command will purge all the messages smaller than 50 KB in size.
sudo ipurge -i -b 50000 /var/spool/cyrus/mail/user/username
Purging Messages Based on Delivery Time
To purge messages from the mailbox based on delivery time, use the -X or --delivery-time option −
sudo ipurge -X 10 /var/spool/cyrus/mail/user/username
The above command searches the specified user's mailbox and deletes all emails delivered more than 10 days ago. Note that it will use the delivery time instead of the Date: header for matching dates.
Getting Verbose Output
To get the verbose output, use the -v or --verbose option with the ipurge command −
sudo ipurge -v -b 50000 /var/spool/cyrus/mail/user/username
Conclusion
The ipurge command in Linux is a tool for mail server administrators using Cyrus to manage IMAP mailboxes by removing emails based on age or size. It offers various options for customization, enabling efficient mailbox maintenance through script integration or manual execution.
Ensuring that Cyrus is installed and configured properly is essential for using the ipurge command. Key functionalities include purging messages based on size, age, and delivery time, as well as the ability to preview actions with a dry run.
In this tutorial, we explained the ipurge command, its installation, syntax, options, and usage with examples.