# win\_file removing root directory when deleting files older than 30 days

**URL:** <https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160>\
**Category:** Ansible Project\
**Created:** [May 12, 2021, 4:48pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160 "2021-05-12T16:48:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jeremy\_Smith](https://forum.ansible.com/u/Jeremy_Smith)\
**Post date:** [May 12, 2021, 4:48pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160/1 "2021-05-12T16:48:45Z")

</div>

Hello All,

Background: We have a “diagnostics” folder on our network that is used to store logs exported from our flagship application.

The problem I’m trying to solve is to remove any files/folders older than 30 days from the “diagnostics” folder.

The following code is being used to perform this:

- name: Find all files older than 30 days in diagnostics folder  
win\_find:  
paths: “{{ unc\_path }}”  
file\_type: directory  
age: 30d  
#recurse: yes  
register: FilesOver30

- name: Delete all files/folders older than 30 days from diagnostics folder  
win\_file:  
path: “{{ unc\_path }}”  
state: absent  
with\_items: “{{ FilesOver30.files }}”

The issue I’m having is, if there are no files newer than 30 days in the “diagnostics” folder, it actually nukes the diagnostics folder itself, which is not the intended result.

Need to be able to have it remove any files/folders older than 30 days from this folder witout nuking the entire thing.

Thanks in advance for the feedback!

---

<div class="post-metadata">

**Author:** ![Jeremy\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jeremy\_Smith](https://forum.ansible.com/u/Jeremy_Smith)\
**Post date:** [May 12, 2021, 4:49pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160/2 "2021-05-12T16:49:46Z")

</div>

For reference, the unc\_path variable seen above is the path to the diagnostics folder.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 12, 2021, 5:32pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160/3 "2021-05-12T17:32:24Z")

</div>

First thing that comes to mind is removing the folder from the  
FilesOver30.files list, for instance using 'difference':  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#selecting-from-sets-or-lists-set-theory](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#selecting-from-sets-or-lists-set-theory)

---

<div class="post-metadata">

**Author:** ![Jeremy\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jeremy\_Smith](https://forum.ansible.com/u/Jeremy_Smith)\
**Post date:** [May 12, 2021, 7:44pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160/4 "2021-05-12T19:44:35Z")

</div>

I’ve read the documentation at the link you provided and am unsure how any of that can help me. There is no other list than FilesOver30.files to compare it with.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 12, 2021, 8:05pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160/5 "2021-05-12T20:05:43Z")

</div>

If I understand your problem description correctly, the value of  
"unc\_path" is in the FIlesOver30.files list.  
You can remove it by the difference of a list containing a single  
item: the unc\_path.

with\_items: "{{ FilesOver30.files | difference([unc\_path]) }}"

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 12, 2021, 9:00pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160/6 "2021-05-12T21:00:01Z")

</div>

You are looking win\_file with the values of FilesOver30.files but setting the path for each of those loops to unc\_path. This will just run the same thing, deleting that UNC path for every loop iteration. What you want to do is set ‘path: “{{ item.path }}”’ so that path is set to each of the found paths that you got in the previous step.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jeremy\_Smith](https://forum.ansible.com/u/Jeremy_Smith)\
**Post date:** [May 12, 2021, 9:20pm UTC](https://forum.ansible.com/t/win-file-removing-root-directory-when-deleting-files-older-than-30-days/35160/7 "2021-05-12T21:20:57Z")

</div>

Thank you, that does seem to have done the trick.
