Linux Delete / Remove User Account Using userdel Command

How do I remove a user’s access from my server? How do I delete a user account under Linux operating systems include home directory and running cron jobs?

You need to use the userdel command to delete a user account and related files from user account under Linux operating system. The userdel command must be run as root user on Linux.

Linux delete user command syntax

The syntax is as follows to remove a user account on Linux.
userdel userName
userdel [options] userName
userdel -r userName

userdel command examples

Let us remove the user named vivek or account named vivek from the local Linux system / server / workstation, enter:
# userdel vivek
Next, delete the user’s home directory and mail spool pass the -r option to userdel for a user named ashish, enter:
# userdel -r ashish

Linux delete user command demo

Explains how to delete user account with home directory in Linux

The above command will remove all files along with the home directory itself and the user’s mail spool. Please note that files located in other file systems will have to be searched for and deleted manually. Pass the -f option for force removal of files, even if not owned by user:
# userdel -r -f tom
To remove any SELinux user mapping for the user pass the -Z option:
# userdel -Z -r -f jerry

  • -f : Delete Linux user account with force removal of files
  • -r : Remove Linux user account including home directory and mail spool
  • -Z : Remove any SELinux user mapping for the user when deleting user from Linux

A Note About /etc/login.defs File

Default values are taken from the information provided in the /etc/login.defs file for RHEL (Red Hat) based distros. Debian and Ubuntu Linux based system use /etc/deluser.conf file:

# /etc/deluser.conf: deluser configuration - Debian / Ubuntu Linux only.
# Remove home directory and mail spool when user is removed
# Remove all files on the system owned by the user to be removed
# Backup files before removing them. This options has only an effect if
# target directory for the backup file
# delete a group even there are still users in this group
# exclude these filesystem types when searching for files of a user to backup
EXCLUDE_FSTYPES = "(proc|sysfs|usbfs|devpts|tmpfs)"

Complete example to remove user account from Linux

The following is recommend procedure to delete a user from the Linux server. First, lock user account, enter:
# passwd -l vivek
OR set the date on which the user account will be disabled (syntax is usermod --expiredate YYYY-MM-DD userNameHere):
# usermod --expiredate 1 vivek
If user try to login, he or she will get the following message:

Your account has expired; please contact your system administrator
Authentication failure

Next, backup files from /home/vivek to /nas/backup
# tar -zcvf /nas/backup/account/deleted/v/vivek.$uid.$now.tar.gz /home/vivek/
Please replace $uid, $now with actual UID and date/time. Tye userdel command will not allow you to remove an account if the user is currently logged in. You must kill any running processes which belong to an account that you are deleting, enter:
# pgrep -u vivek
# ps -fp $(pgrep -u vivek)
# killall -KILL -u vivek

Delete at jobs, enter
# find /var/spool/at/ -name "[^.]*" -type f -user vivek -delete
Remove cron jobs, enter:
# crontab -r -u vivek
Delete print jobs, enter:
# lprm vivek
To find all files owned by user vivek, enter:
# find / -user vivek -print
You can find file owned by a user called vivek and change its ownership as follows:
# find / -user vivek -exec chown newUserName:newGroupName {} \;
Finally, delete user account called vivek, enter:
# userdel -r vivek
Sample session:

Fig.01: Delete User Accounts with Home Directory and All Data In Linux

Fig.01: Delete User Accounts with Home Directory and All Data In Linux

You can automate the entire procedure by writing a shell script (to remove any at/cron/print/file jobs etc), which is left as an exercise to the readers.

See also:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 13 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • logicos Jun 26, 2011 @ 17:41

    The “userdel” command must be use at the end… not a the beginning.

  • Server management Oct 2, 2011 @ 19:02

    Before deleting a user (userdel) you should make sure that no processes running from this user. Otherwise, you can’t delete this user.

    • Umair Nov 27, 2014 @ 22:48


      How would you kill all the process from a user before deleting?

  • Mike DePaulo Feb 29, 2012 @ 18:36

    I believe you are mistaken about ubuntu/debian & deluser.conf.
    basically, “adduser” =! “useradd”

    Ubuntu & debian have two config files:
    which configure these 4 high-level debian-specific account management utilities

    Debian/Ubuntu still have useradd & userdel, but I don’t see those options in my login.defs & Debian officially discourages the use of useradd & userdel anyway.

  • bagheera Jul 3, 2012 @ 9:21

    thanks. this helped.

  • Joshua Garde Aug 1, 2012 @ 10:16

    Thanks man! All your tutorials really help me when I’m managing my Linux systems. +1’d

  • Vineet Roy Sep 2, 2012 @ 12:56

    Really, it helped . Thanks……..

  • Sushil Panchal Jan 29, 2013 @ 18:47

    very useful notes

  • Magesh M Jan 3, 2014 @ 9:58

    @ Server management,

    We can use the “userdel -rf username” to do it.

  • L. Priyambodo Jan 28, 2014 @ 0:40

    I cannot kill
    username 2516 1602 0 01:32 ? 00:00:00 php-fpm: pool username

    everytime I kill it, it spawned with new PID
    Is it safe to stop php5-fpm first then delete the username?

  • Wolf Sep 11, 2015 @ 16:42

    Article refers to disabling user account with passwd -l. This will stop someone’s password from working but if they have access to the machine by some other auth method this won’t prevent access. According to passwd manpage, you should instead use usermod –expiredate 1 (this set the account’s expire date to Jan 2, 1970)

  • shashi Apr 14, 2016 @ 6:06

    Nice tutorial, thanks .

  • Neemaliy Mar 3, 2021 @ 12:56

    Good doc about how to delete user linux command. thank u.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum