'Delete user folder on windows using ansible

Unable to delete user directory using ansible win_file module, there is no error but it is not deleting

  win_file:
    path: C:\Users\myuser
    state: absent
    force: yes

Below one also I have tried where it tries to delete the current user folder and returns directory in use cos it is trying to delete the current user folder

- name: Remove directory structur
  win_file:
    path: C:\Users
    name: myuser
    state: absent
    force: yes



Solution 1:[1]

It is best to delete the profile using a tool designed specifically for that purpose. This will help avoid cluttering the Windows registry and running into any number of other issues.

Check out this Ansible community module: https://docs.ansible.com/ansible/latest/collections/community/windows/win_user_profile_module.html

- name: Remove a profile for a still-existing account
  community.windows.win_user_profile:
    username: myuser
    state: absent

If the account has already been deleted, you need to specify 'name' instead of 'username':

- name: Remove a profile for a deleted account
  community.windows.win_user_profile:
    name: myuser
    state: absent

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1