Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

userdel

Delete a user account, and with -r also its home folder and mail.

Level
RHCSA exam
Group
Users and groups
Needs root
Yes
Package
shadow-utils

What it does

userdel removes the user from the account files, and the private group of the user if no other user has it as primary group.

Without -r the home folder stays. Files of the user in other places always stay: they then show a number instead of a name as owner. A new user with the same UID would own them, so find and clean them first.

How to write it

userdel [OPTION]... LOGIN

Options 6

Most used

-r, --remove
Also remove the home folder and the mail spool.
-f, --force
Remove even when the user is logged in, and remove the group even if it is the primary group of another user. Dangerous.
-Z, --selinux-user
Remove the SELinux user mapping of the login.
-R, --root DIR
Work in this chroot folder.
-P, --prefix DIR
Use the files under this prefix folder.
-h, --help
Show help.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Delete a user and the home folder

    userdel -r contractor1

    The usual way for a lab or a test account.

  2. 2Delete only the account

    userdel olduser

    The files in /home/olduser stay (for a backup).

  3. 3Find files of a user before you delete it

    find / -xdev -user olduser 2>/dev/null

    Keep or move important files first.

  4. 4Find files without an owner after a delete

    find / -xdev -nouser 2>/dev/null

    A security check: orphan files can be taken over by a new user with the same UID.

  5. 5Check that nobody uses the account

    who | grep olduser; pgrep -u olduser

    userdel refuses when the user has processes (exit code 8).

  6. 6Stop the processes, then delete

    pkill -KILL -u olduser; userdel -r olduser

    Only when you are sure the work is saved.

  7. 7Offboarding: lock first, delete later

    usermod -L -e 1 olduser

    Wait some weeks, then userdel -r olduser.

  8. 8Remove the cron jobs of the user

    crontab -r -u olduser

    userdel does not always remove them on every system.

  9. 9Archive the home folder before the delete

    tar -czf /root/olduser-home.tgz -C /home olduser && userdel -r olduser

    Keep a copy for the legal hold period.

  10. 10Delete a service account

    userdel myapp

    Its data in /var/lib/myapp stays; remove it yourself.

  11. 11Check that it is gone

    id olduser
    id: 'olduser': no such user

    The account is removed.

Watch out

  • Files outside the home folder are not removed.
  • -f can remove a group that other users still use as primary group.

Files

/etc/passwd, /etc/shadow, /etc/group, /etc/gshadow
The account files.
/etc/login.defs
USERGROUPS_ENAB decides if the private group is removed.

Exit codes

0 :: Done. 1 :: Could not update the password file. 2 :: Wrong syntax. 6 :: The user does not exist. 8 :: The user is logged in. 10 :: Could not update the group file. 12 :: Could not remove the home folder.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man 8 userdel