# delete the older files using Ansible play book

**URL:** <https://forum.ansible.com/t/delete-the-older-files-using-ansible-play-book/25328>\
**Category:** Ansible Project\
**Created:** [August 25, 2017, 7:57am UTC](https://forum.ansible.com/t/delete-the-older-files-using-ansible-play-book/25328 "2017-08-25T07:57:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bibhu](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bibhu](https://forum.ansible.com/u/bibhu)\
**Post date:** [August 25, 2017, 7:57am UTC](https://forum.ansible.com/t/delete-the-older-files-using-ansible-play-book/25328/1 "2017-08-25T07:57:24Z")

</div>

I need to write an ansible script to solve the below requirement. i am trying to write script, few steps i have written but i am having some doubt how to lsit all the files older than 7 days, once we got the list of fies we need to send those files “\*.merged” to a specific mail id, below are few steps which I have initiated . not sure that is right or wrong. Request you to suggest the steps.

**REQUIREMENT:**

1. monitoring the “\*.merged” files older than 7 days and delete those files after sending the list of files to the Mail id

2. In linux we are using " ls -l /prod/HDOP/data/_/bigplay.na/yarn/nm/usercache" to display all the Cache Files as "_.merged"

3. we need to find all the files older than 7 days and make a list

4. Send a mail having list of files which is older than 7 days to [XXX@gmail.com](mailto:XXX@gmail.com) on a weekly basis.

5.we need to delete all files older than 7 days ,Once we have sent those file list to [XXX@gmail.com](mailto:XXX@gmail.com)

**PLAYBOOK:**

---

<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:** [August 25, 2017, 11:15am UTC](https://forum.ansible.com/t/delete-the-older-files-using-ansible-play-book/25328/2 "2017-08-25T11:15:31Z")

</div>

Pseudo code:

1. find - to find the specific files, and register the result.
2. [http://docs.ansible.com/ansible/latest/mail\_module.html](http://docs.ansible.com/ansible/latest/mail_module.html) and attach the files from the previous task result
3. file - to remove the same set of files from 1.

---

<div class="post-metadata">

**Author:** ![bibhu](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bibhu](https://forum.ansible.com/u/bibhu)\
**Post date:** [August 28, 2017, 6:07pm UTC](https://forum.ansible.com/t/delete-the-older-files-using-ansible-play-book/25328/3 "2017-08-28T18:07:10Z")

</div>

Hello Group Members,

Can some one please look into below ansible codes ,and let me know whether it’s correct or we need to modify anywhere, as I am new to ansible and currently I don’t have access to test the code.

**REQUIREMENT:**

1. monitoring the “\*.merged” files older than 7 days and delete those files after sending the list of files to the Mail id

2. In linux we are using " ls -l /prod/HDOP/data/_/[bigplay.na/yarn/nm/usercache](http://bigplay.na/yarn/nm/usercache)" to display all the Cache Files as "_.merged"

3. we need to find all the files older than 7 days and make a list

4. Send a mail having list of files which is older than 7 days to [XXX@gmail.com](mailto:XXX@gmail.com) on a weekly basis.

5.we need to delete all files older than 7 days ,Once we have sent those file list to [XXX@gmail.com](mailto:XXX@gmail.com)

**PLAYBOOK:**

---

<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:** [August 28, 2017, 10:16pm UTC](https://forum.ansible.com/t/delete-the-older-files-using-ansible-play-book/25328/4 "2017-08-28T22:16:31Z")

</div>

Hi Bibhu

Not sure what you mean by "I don't have access to test the code".  
Instead of guessing what might have happened if you ran the playbook,  
just run it.

If you are concerned with accidentally removing files, use the  
'--check' option for ansible-playbook.  
Ansible will report any issues, usually in a easy to understand  
fashion so you can fix the code.

Dick

---

<div class="post-metadata">

**Author:** ![bibhu](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bibhu](https://forum.ansible.com/u/bibhu)\
**Post date:** [August 29, 2017, 1:51am UTC](https://forum.ansible.com/t/delete-the-older-files-using-ansible-play-book/25328/5 "2017-08-29T01:51:25Z")

</div>

Thank you Dick

Thanks and Regards  
Bibhu
