# Ansible way to delete all files in a remote directory

**URL:** <https://forum.ansible.com/t/ansible-way-to-delete-all-files-in-a-remote-directory/12495>\
**Category:** Ansible Project\
**Created:** [March 13, 2013, 8:37pm UTC](https://forum.ansible.com/t/ansible-way-to-delete-all-files-in-a-remote-directory/12495 "2013-03-13T20:37:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [March 13, 2013, 8:37pm UTC](https://forum.ansible.com/t/ansible-way-to-delete-all-files-in-a-remote-directory/12495/1 "2013-03-13T20:37:20Z")

</div>

I know I can easily do:  
shell: rm -f /etc/whatever/\*

or possibly via a register variable on  
shell: ls -1 /etc/whatever/\*  
register: dircontents

I had assumed I would be able to do this in Ansible trivially without templating, but am not finding it straightforward

- with\_glob enumerates “local” files only
- file: doesn’t take wildcards as far as I know

Is there something obvious I’m missing, or are the above the available implementation-patterns for this?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 13, 2013, 11:21pm UTC](https://forum.ansible.com/t/ansible-way-to-delete-all-files-in-a-remote-directory/12495/2 "2013-03-13T23:21:51Z")

</div>

Just call shell.

---

<div class="post-metadata">

**Author:** ![Luke\_Tislow](https://avatars.discourse-cdn.com/v4/letter/l/ba9def/32.png) [@Luke\_Tislow](https://forum.ansible.com/u/Luke_Tislow)\
**Post date:** [March 14, 2013, 3:31pm UTC](https://forum.ansible.com/t/ansible-way-to-delete-all-files-in-a-remote-directory/12495/3 "2013-03-14T15:31:59Z")

</div>

Would the file module work with state=absent?

---

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [March 15, 2013, 1:19pm UTC](https://forum.ansible.com/t/ansible-way-to-delete-all-files-in-a-remote-directory/12495/4 "2013-03-15T13:19:38Z")

</div>

The file module targets a specific file (unless used with with\_items)  
It can target a directory (recursively with recurse=yes in 1.1), but then the directory gets removed

---

<div class="post-metadata">

**Author:** ![Mike\_Biglan](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mike\_Biglan](https://forum.ansible.com/u/Mike_Biglan)\
**Post date:** [February 17, 2015, 6:35am UTC](https://forum.ansible.com/t/ansible-way-to-delete-all-files-in-a-remote-directory/12495/5 "2015-02-17T06:35:37Z")

</div>

Just tried this and it can’t both use state=absent AND recurse=yes. Only way I could make this happen is drop down to “command”.
