# best practices for removing files in a dir when using  Ansible

**URL:** <https://forum.ansible.com/t/best-practices-for-removing-files-in-a-dir-when-using-ansible/34682>\
**Category:** Ansible Project\
**Created:** [February 25, 2021, 3:45pm UTC](https://forum.ansible.com/t/best-practices-for-removing-files-in-a-dir-when-using-ansible/34682 "2021-02-25T15:45:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paneva](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@paneva](https://forum.ansible.com/u/paneva)\
**Post date:** [February 25, 2021, 3:45pm UTC](https://forum.ansible.com/t/best-practices-for-removing-files-in-a-dir-when-using-ansible/34682/1 "2021-02-25T15:45:08Z")

</div>

Hi Ansible wizards!  
I am currently working on few tasks in a playbook that are going to remove old files. There is a set number of files that I need to keep, say 3 (most recent), and the rest should be removed. I am doing some math magic in Ansible and some shell commands to achieve this. I can’t help but wonder, however, if this a “recommended” way of doing this kind of thing. It seems to me that I am doing too many tasks to achieve this and I begin to wonder if I am making this more complicated than it should be. Thanks in advance !!!  
Here is what I have:

- name: Get list of txt all files in the directory - done for visibility  
shell:  
cmd: “ls -t \*.txt”  
chdir: “{{a\_dir}}”  
register: list\_all\_files

- name: Get total number of txt files in this dir  
shell:  
cmd: " ls -t \*.txt | wc -l "  
chdir: “{{a\_dir}}”  
register: total\_number\_files

- set\_fact:  
number\_of\_files\_to\_keep: “3”  
curr\_number\_files: “{{total\_number\_files.stdout}}”  
num\_files\_to\_remove: “{{(total\_number\_files|int) - (number\_of\_files\_to\_keep |int)}}”

- name: Store the files to be removed in a list  
shell:  
cmd: “ls -t | tail -{{num\_files\_toremove}}”  
register: list\_files\_to\_remove

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 25, 2021, 4:17pm UTC](https://forum.ansible.com/t/best-practices-for-removing-files-in-a-dir-when-using-ansible/34682/2 "2021-02-25T16:17:22Z")

</div>

> Hi Ansible wizards!  
> I am currently working on few tasks in a playbook that are going to remove old files. There is a set number of files  
> that I need to keep, say 3 (most recent), and the rest should be removed. I am doing some math magic in Ansible and some  
> shell commands to achieve this. I can't help but wonder, however, if this a "recommended" way of doing this kind of  
> thing. It seems to me that I am doing too many tasks to achieve this and I begin to wonder if I am making this more  
> complicated than it should be. Thanks in advance !!!  
> Here is what I have:  
> -name: Get list of txt all files in the directory - done for visibility  
> shell:  
> cmd: "ls -t \*.txt"  
> chdir: "{{a\_dir}}"  
> register: list\_all\_files
> 
> - name: Get total number of txt files in this dir  
> shell:  
> cmd: " ls -t \*.txt | wc -l "  
> chdir: "{{a\_dir}}"  
> register: total\_number\_files
> 
> ```
> \- set\_fact: 
> number\_of\_files\_to\_keep: &quot;3&quot;
> curr\_number\_files: &quot;\{\{total\_number\_files\.stdout\}\}&quot;    
> num\_files\_to\_remove: &quot;\{\{\(total\_number\_files|int\) \- \(number\_of\_files\_to\_keep |int\)\}\}&quot;  
> 
> \- name: Store the files to be removed in a list 
> shell:
> cmd: &quot;ls \-t | tail \-\{\{num\_files\_toremove\}\}&quot;   
> register: list\_files\_to\_remove
> 
> ```

\* Using shell module is bad practice (only needed in corner cases)  
\* List of files can be gathered by the find module

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 25, 2021, 4:31pm UTC](https://forum.ansible.com/t/best-practices-for-removing-files-in-a-dir-when-using-ansible/34682/3 "2021-02-25T16:31:19Z")

</div>

Role i created for this purpose:  
[https://github.com/bcoca/ansible-tidy](https://github.com/bcoca/ansible-tidy)

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [February 25, 2021, 5:01pm UTC](https://forum.ansible.com/t/best-practices-for-removing-files-in-a-dir-when-using-ansible/34682/4 "2021-02-25T17:01:56Z")

</div>

Yeah, that’s a big mess that avoids using most features that ansible provides. Even if you want to stick to the `ls` approach, there’s no reason to call it so many times.

- name: Get list of all txt files in the directory  
shell:  
cmd: ls -t \*.txt  
chdir: “{{ a\_dir }}”  
register: list\_all\_files

- name: Store the files to be removed in a list  
set\_fact:  
list\_files\_to\_remove: “{{ list\_all\_files.stdout\_lines[(number\_of\_files\_to\_keep | int):] }}”  
vars:  
number\_of\_files\_to\_keep: “3”

More idiomatic would be to use the `find` module:

- name: Find existing text files  
find:  
path: “{{ a\_dir }}”  
pattern: “\*.txt”  
register: result

- name: Remove all but 3 files  
file:  
dest: “{{ item.path }}”  
state: absent  
loop: “{{ (result.files | sort(attribute=‘mtime’))[:-3] }}”
