How to delete multiple files / directories in Ansible

There are multiple methods in Ansible by which you can delete a particular file or directory, delete all files in a directory, delete files using regex etc. The safe way is to use the Ansible file module. You can also use the shell module to achieve the task. But it is not idempotent and hence re-execution will throw errors.

Deleting a file

You can use the Ansible file module to delete a particular file safely. The module provides the parameter state with option absent to remove the file from the target machine. This can also be used to delete directories or symlinks.

The following example will remove the delete.conf file from the target system. If it is already absent, then no action is taken.

- name: Ansible delete file example
  file:
    path: /etc/delete.conf
    state: absent

Deleting multiple files

You can delete multiple files by iterating with Ansible loop. You need to specify all the files to be removed under with_items.The following example will delete all the files given under with_items.

The following example will delete the three files given under with_items.

- name: Ansible delete multiple file example
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - hello1.txt
    - hello2.txt
    - hello3.txt

Note 1: Make sure the with_items is on the level of the module name, in this case, file module. Else Ansible will throw an error during execution.

Ansible delete a directory/folder

If you give a directory name against the path parameter, the directory will be removed if it exists, along with all the files.

The following example will delete the removed_files directory. If the directory is not present, no error will be thrown.

- name: Ansible delete directory example
  file:
    path: removed_files
    state: absent

Delete files using file glob

Sometimes you might need to delete all files having a particular format. Wildcards can be useful in such situations. But the file module does not provide any support for wildcards.

One method to achieve this is by using the shell module. You can use the rm -rf command to delete the files, as you would do in the normal Linux operation. Since we are using rm -rf, it will not throw any error even if the file is not present.

- name: Ansible delete file wildcard example
  shell: rm -rf hello*.txt

A better option is to first find all the files which match the wildcard, store it and delete each of them.

In the following example, we use the find module to find all the files with .txt extension. By default, the patterns are file globs. If you need more complex regex, you can use python regex. You just need to set the parameter use_regex to true. The find module returns all the files which match the pattern and they are stored in a register.

The find module returns all the files which match the pattern and they are stored in the register files_to_delete.

In the next task, we iterate through the files using ‘register.files’, deleting each file one by one.

- hosts: all
  tasks:
  - name: Ansible delete file glob
    find:
      paths: /etc/Ansible
      patterns: *.txt
    register: files_to_delete

  - name: Ansible remove file glob
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ files_to_delete.files }}"

Delete files using wildcards

As mentioned above, the patterns are file globs by defaults. You can also use the Python regex to find the required files. For setting the Python regex as the pattern matcher, you need to set the parameter use_regex to true. By default, it is set to false.
In the following example, all .txt files starting with ‘he’ are deleted.

- hosts: all
  tasks:
  - name: Ansible delete file wildcard
    find:
      paths: /etc/wild_card/example
      patterns: "^he.*.txt"
      use:regex: true
    register: wildcard_files_to_delete

  - name: Ansible remove file wildcard
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ wildcard_files_to_delete.files }}"

Remove files older than a date

We can use the find module to retrieve all the files that are older than a particular time and then delete those files using file module. This can be helpful when dealing with logs.

In the following example, the first task finds all files older than 5 days. The second task will delete all these files.

- hosts: all
  tasks:
  - name: Ansible delete files older than 5 days example
    find:
      paths: /Users/dnpmacpro/Documents/Ansible
      age: 5d
    register: files_to_delete

  - name: Ansible remove files older than a date example
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ files_to_delete.files }}"