delete all files under a directory

How do I delete all files under a directory?

use command or shell module

regards,

Even better. Use file module

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

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

scroll to the bottom… it has your example:

Using the Ansible find module to search for files/folder - My Daily Tutorials

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 }}"