Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible : delete all files and directories inside a folder and not the folder itself

i'm trying to run a task which deletes all the content of my project fiolder , without deleting the folder itself:

my folder : appFolder ----contains----> file1 , file2 ... fileN , directory1 ...directoryN

i ve tried to use ls , fileGlob , but that didn't work properly.

- name: delete old version files
  file:
   path: "{{ paths }}"
   state: absent
  with_filesglob:
   - "{{paths}}deletes/*"

also with shell module (not good because it didn't delete file with special characters names)

- shell: ls -1 /some/dir
  register: contents

- file: path=/some/dir/{{ item }} state=absent
  with_items: {{ contents.stdout_lines }}

A better solution ?

like image 631
firasKoubaa Avatar asked Nov 16 '25 04:11

firasKoubaa


1 Answers

Until #18910 is fixed (state=empty for dirs), we are stuck with workarounds.

Here's one of them:

- name: Clean build folder
  shell: cd /project/build && find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf --
like image 128
Konstantin Suvorov Avatar answered Nov 18 '25 20:11

Konstantin Suvorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!