Delete a user with userdel, and choose whether to keep or remove their files
To delete a user in Linux, open a terminal and run userdel followed by the username. The command userdel username removes the user from the system files that track who can log in, but leaves their home directory and files behind. If you want to delete the user and remove everything they owned — their home folder, mail spool, and all files — use userdel -r username instead. The -r flag stands for "remove" and deletes the home directory in one step.
You must have root or sudo access to run userdel. If you are not logged in as root, prefix the command with sudo: sudo userdel -r username. The system will prompt you for your password if you have not used sudo recently. After the command completes, the user cannot log in, and depending on which flag you used, their files either remain on disk or are gone.
Key Takeaways
- Use userdel username to remove the user account but keep their home directory and files on disk.
- Use userdel -r username to delete the user account and remove their home directory, mail, and all owned files at once.
- You need root or sudo access to run userdel; prefix the command with sudo if you are not logged in as root.
- Before deleting a user, check what files they own with find / -user username so you know what will be removed or left behind.
- Deleting a user does not automatically stop running processes owned by that user; use killall -u username to stop them first.
Check what files the user owns before you delete them
Before you run userdel, search the system to see what the user owns. Run find / -user username to list every file and directory owned by that user. This search takes a minute or two on a full system, but it shows you exactly what will be deleted if you use the -r flag, or what will be left behind if you do not.
If the user owns important files outside their home directory — perhaps a database, a web process, or shared project files — you may want to change ownership of those files to another user before deleting the account. Use chown newuser filename to transfer ownership. This way the files stay on the system and remain accessible, but they no longer belong to the deleted user.
Stop the user's running processes first
If the user is currently logged in or has background processes running, those processes will keep running even after you delete the user account. To stop all processes owned by a user, run killall -u username. This sends a termination signal to every process the user started.
If you are deleting a service account — a user created to run a specific process like a web server or database — check whether any critical services depend on that account. Some applications run under their own user for security reasons, and deleting that user will break the process. Review what the user was created for before you proceed.
The difference between userdel and userdel -r
userdel without the -r flag removes the user from the password file, shadow file, and group files. The user cannot log in anymore, and their name no longer appears in system records. However, their home directory stays on disk with all its contents. This is useful if you want to preserve the user's files for archival, transfer to another account, or investigation.
userdel -r does the same thing but also deletes the home directory and the user's mail spool (usually stored in /var/mail/). This is a permanent deletion — the files are removed from disk and cannot be recovered through normal means. Use this flag only when you are certain the user's files are no longer needed.
A third option is to use userdel without -r, then manually delete the home directory later with rm -r /home/username. This gives you time to back up or review the files before they are gone.
Verify the user is deleted and check for leftover files
After running userdel, confirm the user no longer exists by trying to view their entry in the password file: grep username /etc/passwd. If the command returns nothing, the user is gone. You can also run id username, which will return an error saying the user does not exist.
If you used userdel without -r, search again for files owned by that user with find / -user username. If the search returns results, those files are still on disk and still owned by the deleted user's ID number (UID). You can delete them manually, change their ownership, or leave them as they are. Files owned by a UID that no longer has a username are sometimes called "orphaned" files.
Delete a user from a specific group without deleting the account
If you only want to remove a user from a group — not delete the user entirely — use gpasswd -d username groupname. This removes the user from that group but leaves the user account intact. The user can still log in and access the system; they straightforward lose the permissions associated with that group.
To see which groups a user belongs to, run groups username. To add a user back to a group later, use gpasswd -a username groupname. These commands are useful for managing access without destroying the account.
Frequently Asked Questions
What happens to files owned by a deleted user?
If you use userdel without -r, the files stay on disk but are now owned by a UID with no username. If you use userdel -r, the home directory and mail spool are deleted when ready. Files outside the home directory are not affected by either command.
Can I recover a deleted user account?
No, userdel does not create a backup. If you deleted the account by mistake, you must recreate it with useradd and assign a new password. The user's UID will be different unless you specify it. If you used -r, the home directory is gone and cannot be recovered through normal means.
What if the user is still logged in when I delete them?
The user can stay logged in and continue using their session, but they cannot log in again after they log out. Their running processes continue until they exit or you kill them with killall -u username. The account is deleted from the system files when ready.
Do I need to delete the user's cron jobs or scheduled tasks?
On most Linux systems, cron jobs are stored in /var/spool/cron/crontabs/ and are deleted automatically when you use userdel -r. If you use userdel without -r, the cron file remains and may cause errors. You can delete it manually with rm /var/spool/cron/crontabs/username.
How do I delete a user on a system with NIS or LDAP?
On systems using network authentication, userdel may not work or may only remove the local copy of the user. Check your system documentation or contact your network administrator. You may need to use a different tool or delete the user from the central directory server instead.