# delete all files under a directory

**URL:** <https://forum.ansible.com/t/delete-all-files-under-a-directory/34377>\
**Category:** Ansible Project\
**Created:** [December 29, 2020, 2:56am UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377 "2020-12-29T02:56:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reneesh\_K](https://avatars.discourse-cdn.com/v4/letter/r/f07891/32.png) [@Reneesh\_K](https://forum.ansible.com/u/Reneesh_K)\
**Post date:** [December 29, 2020, 2:56am UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377/1 "2020-12-29T02:56:07Z")

</div>

How do I delete all files under a directory?

---

<div class="post-metadata">

**Author:** ![Tej-Singh-Rana](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@Tej-Singh-Rana](https://forum.ansible.com/u/Tej-Singh-Rana)\
**Post date:** [December 29, 2020, 3:01am UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377/2 "2020-12-29T03:01:17Z")

</div>

use command or shell module

regards,

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [December 29, 2020, 3:02am UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377/3 "2020-12-29T03:02:28Z")

</div>

Even better. Use file module

---

<div class="post-metadata">

**Author:** ![Reneesh\_K](https://avatars.discourse-cdn.com/v4/letter/r/f07891/32.png) [@Reneesh\_K](https://forum.ansible.com/u/Reneesh_K)\
**Post date:** [December 29, 2020, 3:06am UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377/4 "2020-12-29T03:06:17Z")

</div>

I used the file module, but it is deleting even the logs directory. This is my code.

- name: empty logs directory  
file:  
path: /app/logs/  
state: absent

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [December 29, 2020, 3:15am UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377/5 "2020-12-29T03:15:42Z")

</div>

Use the find module to list the files you want /register the files and then loop the car to remove the file in file module

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [December 29, 2020, 3:19am UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377/6 "2020-12-29T03:19:41Z")

</div>

scroll to the bottom… it has your example:

[Using the Ansible find module to search for files/folder - My Daily Tutorials](https://www.mydailytutorials.com/using-ansible-find-module-search-filesfolder/)

---

<div class="post-metadata">

**Author:** ![Reneesh\_K](https://avatars.discourse-cdn.com/v4/letter/r/f07891/32.png) [@Reneesh\_K](https://forum.ansible.com/u/Reneesh_K)\
**Post date:** [December 30, 2020, 7:49pm UTC](https://forum.ansible.com/t/delete-all-files-under-a-directory/34377/7 "2020-12-30T19:49:01Z")

</div>

Hi Wei,

If you are referring to this code, then it is deleting the destination folder too.

```auto
- hosts: all
  tasks:
  - name: Ansible delete files examples
    find:
      paths: /home/mdtutorials2/findmodule/examples
      patterns: "*.log"
      age: "100d"
      recurse: yes
      register: files_to_delete

  - name: Ansible delete file glob
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ files_to_delete.files }}"

```
