Delete all root mail / inbox on Linux / Unix from a shell prompt

I have CentOS Linux acting as a router for our small community-based college. I see logwatch and other cron job generating emails for the root account. How do I delete those emails? I don’t want to disable email facility but just wanted to get rid of all root emails. How do I delete root user mails (mailbox) file in Linux or Unix like system?

The easiest way is to empty root or users email message file. The file is located at/var/spool/mail/root or /var/spool/mail/username location. You can read mail using mail/mailx command. It is an intelligent mail processing system, which has a command syntax reminiscent of ed with lines replaced by messages.

ADVERTISEMENTS

How to install mail command on Linux

Type the following yum command on a CentOS/RHEL to install the same:
sudo yum install mailx
Use the following dnf command on a Fedora Linux to install it:
sudo dnf install mailx
Try the following apt command/apt-get command on an Ubuntu or Debian system:
sudo apt install mailutils

How do I read my mail messages from the CLI on Linux?

Simply type the following command:
mail
OR
mailx
How to view root user email on Linux or Unix using mail command
The current message is initially the first message (numbered 1) and can be printed using the print command which can be abbreviated p. Just press p. The user can move among the messages much as he moves between lines in ed, with the commands + and - moving backwards and forwards, and simple numbers. Just type 10 to read email message # 10. After viewing a message the user can delete by pressing d. To reply press r.

How to delete root user’s mailbox/inbox file in Linux or Unix

Simply type the following command at shell prompt to delete all root mail:
> /var/spool/mail/root
Verify it with the following command or cat command or ls command:
mail
cat /var/spool/mail/root
ls -l /var/spool/mail/root

How to delete all root mail command in Linux or Unix

How to remove root user’s email every day using cron job

Simply run the following cron job:
@daily > /var/spool/mail/root

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
27 comments… add one
  • Nirmal Pathak Feb 19, 2009 @ 16:03

    Nothing happens when I type /var/spool/mail/root to purge the root user’s mail messages.

    I think you meant,

    #cat /dev/null > /var/spool/mail/root

    — Nirmal.

    • Jorge Tarlea Oct 12, 2010 @ 11:01

      Thanks, it worked!

    • Adam Ziaja Sep 28, 2011 @ 10:15

      because you need type > before file to clear…

  • Cian Mar 31, 2009 @ 15:55

    Cheers, had forgotten all anout dev/null, thanks!

  • Kaspar Sep 10, 2009 @ 20:00

    You just did oversee the > in front of /var/spool/mail/root
    Works like a charm…

  • Md. Mizanur Rahman Nov 4, 2009 @ 8:44

    Thank boss……..it’s an important command

  • Sunil Apr 21, 2010 @ 7:15

    Great thanks for the command

  • //Sessl Dec 10, 2010 @ 14:39

    mail
    d 1-x

    d = delete
    1-x = mail 1 to mail x (e.g. d 1-53)

  • Khupcom Dec 12, 2010 @ 22:55

    Here the simple way to delete all mail
    echo ‘d *’ | mail -N

    • fukid Oct 14, 2011 @ 5:04

      Thanks!
      It works!!

    • wolvies Dec 12, 2011 @ 9:29

      I’ve tried that but received this message: ‘Value too large for defined data type’ :-(
      can anybody help me?

      thanks from now !!!

  • baanmaha Apr 9, 2011 @ 1:54

    Thank you for advice worked..

  • Santosh Sheelvant Jun 2, 2011 @ 7:36

    I have deleted the file “root” from /var/spool/mail itself and from then on nobody is receiving the emails upon our transactions.

    Please suggest me a solution if any?

    • Jack O'Lantern Jul 30, 2015 @ 14:43

      Issue this command, as root:

      touch /var/spool/mail/root

  • linux course Jul 19, 2011 @ 16:28

    gud information.worked great.
    Thanks….

  • Mars Jan 25, 2012 @ 21:54

    Use Nirmal’s solution above:

    `cat /dev/null > /var/spool/mail/root`

  • J Feb 27, 2012 @ 10:28

    Not the correct way.

    Use biff or edit your .bashrc to add:

    unset MAILCHECK

  • James Oct 9, 2012 @ 11:05

    You can’t cat /dev/null; it is a block special device. It also outputs nothing and closes right away; it is the same as the original post in concept. the Null device is a place to output stuff you don’t want, its opposite would be ‘/dev/zero’. The original one doesn’t work because it is unbalanced- there is nothing going into the file.

    ‘exit > /path/to/file’

    Is basically what the cat /dev/null does. You want

    echo > /path/to/file. instead.

  • adiss Feb 9, 2013 @ 10:26

    to empty the mail file

    #cp /dev/null /var/mail/root

  • banjo May 14, 2013 @ 4:24

    Can i eat near my Linux server or is this bad practice?

  • Paul Thomson Oct 5, 2013 @ 19:04

    Fantastic! – Just cleared 35000 unread mail items ;-)

    Keep up the good work, stay safe!
    Paul :-)

  • Dave Mar 2, 2014 @ 1:59

    none of this works

  • Aijaz Mar 16, 2014 @ 7:25

    what will be the impact if I use the command for oracle file which is more than 3 gb .If there is no bad impact ,can i execute on Production server,can I get any script

    cat /dev/null > var/spool/mail/oracle

    • Jackk O'Lantern Jul 30, 2015 @ 14:47

      This will delete the mail messages for user ‘oracle’, leaving the file intact as a zero-byte file. If you don’t need the messages, this is safe to do. HOWEVER, you should get into the habit of saving this file off somewhere before you issue this command (ftp the file to your local machine, then put it on a disk or something…just in case you need to refer to it later).

  • vietiso Apr 4, 2014 @ 9:04
  • murty Jun 24, 2014 @ 4:52

    it works…

  • mf-service Nov 11, 2015 @ 11:41

    try
    sudo mail -u username
    On mail-promt type
    :d *
    :q

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.