Bootstrap FreeKB - Ansible - Remove files and directories using the file module
Ansible - Remove files and directories using the file module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The file module with state: absent can be used to remove a file or a directory (and the contents of the directory). If the file or directory does not exist, the task will be skipped, thus there is no need to check if the file or directory exists.


remove / delete a single file or directory

In this example, /tmp/foo will be removed if it exists. It does not matter if /tmp/foo is a file or directory. Ansible should remove /tmp/foo regardless if /tmp/foo is a file or a directory. Be aware that if /tmp/foo is a directory, all of the contents of /tmp/foo will be removed.

---
- hosts: all
  tasks:
  - name: delete /tmp/foo
    file:
      path: /tmp/foo
      state: absent
    register: out

  - debug:
      var: out
...

 

Something like this should be returned.

ok: [server1.example.com] => {
    "remove": {
        "changed": true, 
        "diff": {
            "after": {
                "path": "/tmp/foo", 
                "state": "absent"
            }, 
            "before": {
                "path": "/tmp/foo", 
                "path_content": {
                    "directories": [
                        "/tmp/foo/dir1", 
                        "/tmp/foo/dir2"
                    ], 
                    "files": [
                        "/tmp/foo/hello.txt", 
                        "/tmp/foo/world.txt", 
                        "/tmp/foo/dir1/hello.txt", 
                        "/tmp/foo/dir2"
                    ]
                }, 
                "state": "directory"
            }
        }, 
        "failed": false, 
        "path": "/tmp/foo", 
        "state": "absent"
    }
}

 


Permission denied

If permission denied is returned when running the play, refer to our article on resolving permission denied.

 


Successfully removed

If the file or directory was successfully removed, the play should indicate changed.

TASK [delete /tmp/foo]
changed: [server1.example.com]

 

Directory not removed

If the file or directory was not removed, the play should indicate ok. This is the expected behavior if the file or directory does not exist.

TASK [delete /tmp/foo]
ok: [server1.example.com]

 


remove / delete multiple files or directories

The with_items parameter can be used to remove multiple files or directories.

- name: remove /tmp/foo and /tmp/bar
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - /tmp/foo
    - /tmp/bar

 


remove files or directories that match a pattern

Let's say you want to remove any file or directory that contains "foo". In this example, the find module with the patterns parameter is used to store all of the files that contain "foo" in the "out" variable.

- name: store all of the files and directories in the /tmp directory that contain 'foo' in the 'out' variable
  find:
    paths: /tmp
    patterns: (?i).*foo.*
    use_regex: true
  register: out

 

Now, you can use the with_items parameter to loop through the out variable, and remove them. Note that the loop_control parameter is optional, not required.

- name: remove files and directories containing 'foo' in the /tmp directory
  file:
    path: "{{ item.path }}"
    state: absent
  with_items: "{{ out.files }}"
  loop_control:
    label: "{{ item.path }}"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


May 31 2023 by Bruno
- name: remove /tmp/foo and /tmp/bar file: path: "{{ items }}" state: absent with_items: - /tmp/foo - /tmp/bar it should be path: "{{ item }}" .

May 31 2023 by Jeremy (moderator)
Good catch. Thanks Bruno. I got this corrected to {{ item}}

Add a Comment


Please enter 0b5d6c in the box below so that we can be sure you are a human.