Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Command

sudo userdel -r deploy

userdel removes the account from /etc/passwd, /etc/shadow, and /etc/group. With -r it also deletes the home directory and the user's mail spool. Without -r the home directory stays on disk. There is no undo, so go through the checks below first.

Before You Delete

  1. Note the user ID: you need it later to find files the account owns outside its home directory.
  2. Look for running processes: userdel refuses to delete an account that still runs something.
  3. Look for scheduled jobs: a crontab or a systemd service that runs as this user will fail after the deletion.
  4. Back up the home directory: keep a copy until you are sure nobody needs the files.
id deploy
ps -u deploy
sudo crontab -l -u deploy
sudo grep -rl "User=deploy" /etc/systemd/system
sudo tar -czf /root/deploy-home.tar.gz -C /home deploy

If processes are running, stop the service that owns them in the normal way. For processes left from a login session, sudo pkill -u deploy sends the polite stop signal; force is the last step, as how to kill a process explains. Remove a crontab you no longer need with sudo crontab -r -u deploy; how to list cron jobs shows the other places where jobs can hide.

deluser on Debian and Ubuntu

On Debian and Ubuntu, deluser is a friendlier front end. It comes with the adduser package, which is normally installed; if the command is missing, install that package:

sudo deluser deploy
sudo deluser --remove-home deploy

The option --remove-all-files deletes every file the user owns anywhere on the system; run it only when you know what that covers. Watch the argument count: sudo deluser deploy sudo does not delete anything, it removes the user deploy from the group sudo. AlmaLinux and Rocky Linux have no deluser; use userdel there.

Find Files Left Elsewhere

userdel -r removes only the home directory and the mail spool. Files under /var/www, /srv, or /tmp stay and are then owned by a bare number. Search by the user ID you noted, or for files without an owner after the deletion:

sudo find / -xdev -uid 1001 -ls
sudo find / -xdev -nouser -ls

Give the files to another account or remove them. A new user created later can receive the same user ID and would then own these files without anyone noticing. Reassigning a whole tree is covered in chown recursive.

Lock Instead of Delete

When a person leaves but their files or setup may still be needed, lock the account rather than delete it:

sudo usermod -L -e 1 deploy

-L locks the password and -e 1 expires the account, which also stops logins with an SSH key. Setting the shell to /usr/sbin/nologin with usermod -s adds a third barrier. Password handling is covered in how to change a user password in Linux.

Remove Admin Rights Only

To keep the account but take away sudo, remove it from the admin group. On Debian and Ubuntu the group is sudo, on AlmaLinux and Rocky Linux it is wheel:

sudo gpasswd -d deploy sudo
sudo gpasswd -d deploy wheel

Also look for rules that name the user directly with sudo grep -r deploy /etc/sudoers.d and change them with visudo, as described in the sudoers file. Sessions that are already open keep the old group list until the user logs out.

Messages You May See

  • The user is currently used by a process: something still runs as this account. Stop it and try again; do not force the deletion.
  • The mail spool was not found: harmless; the account never received local mail.
  • The group was not removed: the user's personal group still has other members or is another account's main group.

Never delete system accounts such as www-data, nginx, or mysql: services and packages depend on them. Check what an account is with how to list users in Linux before you remove it.

Was this answer helpful?
Loading...