delete all files under a directory

39 views
Skip to first unread message

Reneesh K

unread,
Dec 28, 2020, 9:56:08 PM12/28/20
to Ansible Project
How do I delete all files under a directory?

Tej Singh Rana

unread,
Dec 28, 2020, 10:01:54 PM12/28/20
to ansible...@googlegroups.com
use command or shell module


regards,


On Tue, Dec 29, 2020 at 8:26 AM Reneesh K <rene...@gmail.com> wrote:
How do I delete all files under a directory?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/65531941-f099-417e-b5e6-3f26f0c96240n%40googlegroups.com.


--
---------------------
Tej Singh Rana

Wei-Yen Tan

unread,
Dec 28, 2020, 10:02:50 PM12/28/20
to ansible...@googlegroups.com

Reneesh K

unread,
Dec 28, 2020, 10:06:54 PM12/28/20
to ansible...@googlegroups.com
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

Best Regards,
Reneesh K



You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/0LPtwD5c2Yw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BjXPxi0zOg0fdpYT4N4uRPjJVuazzX_m53gcY%2B-Rr1nyq_gTw%40mail.gmail.com.

Wei-Yen Tan

unread,
Dec 28, 2020, 10:16:08 PM12/28/20
to ansible...@googlegroups.com

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 

Wei-Yen Tan

unread,
Dec 28, 2020, 10:20:07 PM12/28/20
to ansible...@googlegroups.com

Reneesh K

unread,
Dec 30, 2020, 2:49:45 PM12/30/20
to ansible...@googlegroups.com
Hi Wei,

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

- 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 }}"
Best Regards,
Reneesh K



Reply all
Reply to author
Forward
0 new messages