# Delete files from directory: delete all files except some special ones?

**URL:** <https://forum.ansible.com/t/delete-files-from-directory-delete-all-files-except-some-special-ones/21143>\
**Category:** Ansible Project\
**Created:** [April 12, 2016, 6:19pm UTC](https://forum.ansible.com/t/delete-files-from-directory-delete-all-files-except-some-special-ones/21143 "2016-04-12T18:19:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [April 12, 2016, 6:19pm UTC](https://forum.ansible.com/t/delete-files-from-directory-delete-all-files-except-some-special-ones/21143/1 "2016-04-12T18:19:32Z")

</div>

Hi all,

excuse the subject, I'll try to explain:

I can tell ansible to delete all files from a certain directory, at  
least I guess I can...

But can I tell it to exclude certain files?

I know I could use the synchronize module, which seems to be rsync in  
disguise. But it seems a bit ugly, to keep around a lot of different  
directories for all different hosts, where there are different files  
in the directory.

Also, using file module to delete the directory and then populating it  
with file or template would report 'changed' each time.

Any hints?

Johannes

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 13, 2016, 5:51pm UTC](https://forum.ansible.com/t/delete-files-from-directory-delete-all-files-except-some-special-ones/21143/2 "2016-04-13T17:51:45Z")

</div>

you can use the ‘find’ module to select the files to be deleted, I have a ‘tidy’ role as an example: [https://galaxy.ansible.com/bcoca/tidy/](https://galaxy.ansible.com/bcoca/tidy/)
