AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Remove User Account: Delete Users with user Module — Video Tutorial
How to remove user accounts with Ansible user module. Delete users, remove home directories, force removal of logged-in users, and manage bulk deprovisioning.
What You'll Learn
- How to remove a user account with Ansible?
- Ansible remove user account
- Main Parameters
- Conclusion
- Advanced User Removal Examples
- Remove user and their home directory
- Force remove user (even if logged in)
- Remove multiple users
- Safe removal with backup
- Remove user from groups without deleting account
Full Tutorial Content
How to remove a user account with Ansible?
I'm going to show you a live Playbook with some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible remove user account
Today we're talking about the Ansible module `user`.
The full name is `ansible.builtin.user`, which means that is part of the collection of modules "builtin" with ansible and shipped with it.
It's a module pretty stable and out for years, it manages user accounts.
It supports a huge variety of Linux distributions, SunOS and macOS, and FreeBSD.
This module uses Linux distributions `userdel` to delete, on FreeBSD, this module uses `pw userdel`, on macOS, this module uses `dscl`.
For Windows, use the `ansible.windows.win_user` module instead.
Main Parameters
- **name** _string_ - username
- state _string_ - present/absent
- remove _boolean_ - **no**/yes
This module has many parameters to perform any task.
The only required is "name", which is the username.
"state" allows us to create or delete a user, in the use case we need to specify "absent" to delete a user.
If we would like to try to remove the directories associated with the user, we need to set the parameter "remove".
The behavior is the same as `userdel --remove`.
Files in the user's home directory will be removed along with the home directory itself and the user's mail spool.
Files in other parts of the file system will have to be searched for and deleted manually.
## Playbook
Let's jump into a real-life Ansible Playbook to delete a user.
- _delete_user.yml_
```yaml
---
- name: user module Playbook
hosts: all
become: true
tasks:
- name: user example not present
ansible.builtin.user:
name: example
state: "absent"
remove: true
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/delete%20user%20account)
Conclusion
Now you know how to remove a user account with Ansible.
Advanced User Removal Examples
Remove user and their home directory
```yaml
- name: Remove user and home directory
ansible.builtin.user:
name: olduser
state: absent
remove: true # Removes home directory and mail spool
become: true
```
Force remove user (even if logged in)
```yaml
- name: Force remove user with active sessions
ansible.builtin.user:
name: olduser
state: absent
remove: true
force: true # Kills user processes and removes even if logged in
become: true
```
Remove multiple users
```yaml
- name: Remove decommissioned users
ansible.builtin.user:
name: "{{ item }}"
state: absent
remove: true
loop:
- contractor1
- contractor2
- temp_user
become: true
```
Safe removal with backup
```yaml
- name: Check if user exists
ansible.builtin.getent:
database: passwd
key: "{{ target_user }}"
register: user_check
failed_when: false
- name: Backup home directory before removal
ansible.builtin.archive:
path: "/home/{{ target_user }}"
dest: "/backup/{{
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 8 min
- Category: troubleshooting
Read the full written article: Ansible Remove User Account: Delete Users with user Module
Related Video Tutorials
- Ansible Create User Account: user Module Complete Guide — How to create user accounts with Ansible user module. Set passwords, SSH keys, groups, shells, home directories, and manage users with examples.
- Ansible Password Expiration: Manage User Account Aging & Policies — How to manage password expiration with Ansible user module. Set expiry dates, maximum age, warning periods, and enforce password rotation policies.
- Ansible Change Windows User Password: win_user Module (Examples) — How to change local Windows user passwords with Ansible win_user module. Reset passwords, set expiry policies, and manage credentials across Windows servers.
- Ansible group Module: Create & Manage Linux Groups (ansible.builtin.group) — How to create and manage Linux groups with Ansible group module (ansible.builtin.group). Add groups, set GID, manage system groups.
- Create Local Groups on Windows with Ansible Playbooks — Learn how to create and manage local groups on Windows systems using Ansible’s win_group module. Follow our detailed Playbook example for automation.
- Ansible Create Windows Local User: win_user Module (Complete Guide) — How to create local user accounts on Windows with Ansible win_user module. Set passwords, groups, permissions, and manage user lifecycle with playbook examples.