# Delete all files under a specific directory

**URL:** <https://forum.ansible.com/t/delete-all-files-under-a-specific-directory/28915>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [November 1, 2018, 4:46am UTC](https://forum.ansible.com/t/delete-all-files-under-a-specific-directory/28915 "2018-11-01T04:46:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_M1](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Michael\_M1](https://forum.ansible.com/u/Michael_M1)\
**Post date:** [November 1, 2018, 4:46am UTC](https://forum.ansible.com/t/delete-all-files-under-a-specific-directory/28915/1 "2018-11-01T04:46:20Z")

</div>

Per the documentation (in bold red below), the file module is working as designed:

If `directory`, all intermediate subdirectories will be created if they do not exist. Since Ansible 1.7 they will be created with the supplied permissions. If `file`, the file will NOT be created if it does not exist; see the `touch`value or the copy or template module if you want that behavior. If `link`, the symbolic link will be created or changed. Use `hard` for hardlinks. **If** ` **absent** ` **, directories will be recursively deleted,** and files or symlinks will be unlinked. Note that `absent` will not cause `file` to fail if the `path` does not exist as the state did not change. If `touch` (new in 1.4), an empty file will be created if the `path` does not exist, while an existing file or directory will receive updated file access and modification times (similar to the way `touch` works from the command line).

You probably want to use [https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html?highlight=with\_fileglob](https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html?highlight=with_fileglob)

So without testing it, it would look something like (run in ‘test mode’ (-C) first!):

- hosts: all  
tasks:

- name: remove web dir contents  
file:  
path: “{{ item }}”  
state: absent

with\_fileglob:

- “/tmp/xyz/\*”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 1, 2018, 7:21am UTC](https://forum.ansible.com/t/delete-all-files-under-a-specific-directory/28915/2 "2018-11-01T07:21:14Z")

</div>

> You probably want to use  
> [https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html?highlight=with\_fileglob](https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html?highlight=with_fileglob)
> 
> So without testing it, it would look something like (run in 'test mode'  
> (-C) first!):
> 
> - hosts: all  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: remove web dir contents  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_fileglob:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "/tmp/xyz/\*"

lookup plugins only work on Ansible controller aka localhost, not on remote hosts.

---

<div class="post-metadata">

**Author:** ![Michael\_M1](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Michael\_M1](https://forum.ansible.com/u/Michael_M1)\
**Post date:** [November 1, 2018, 10:40pm UTC](https://forum.ansible.com/t/delete-all-files-under-a-specific-directory/28915/3 "2018-11-01T22:40:22Z")

</div>

My bad, thanks for pointing that out.
