Ansible Delete File: Remove Files & Directories with file Module
By Luca Berton · Published 2024-01-01 · Category: installation
How to delete files and directories in Ansible with the file module state=absent. Remove single files, directories, wildcards with find module.
To delete files and directories in Ansible, use ansible.builtin.file with state: absent. It's idempotent — running it twice won't cause errors if the file is already gone.
Delete a Single File
- name: Remove temporary file
ansible.builtin.file:
path: /tmp/installer.sh
state: absent
See also: Ansible Delete Files in Directory: find & file Modules (Examples)
Delete a Directory (Recursively)
- name: Remove build directory
ansible.builtin.file:
path: /opt/app/build
state: absent
This removes the directory and everything inside it — equivalent to rm -rf.
Delete a Symlink
- name: Remove symbolic link
ansible.builtin.file:
path: /opt/app/current
state: absent
See also: Ansible Set File Permissions 755: chmod with file Module Guide
Delete Multiple Files
- name: Remove multiple files
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop:
- /tmp/installer.sh
- /tmp/setup.log
- /var/cache/myapp/temp.db
Delete Files Matching a Pattern
Use ansible.builtin.find + ansible.builtin.file together:
- name: Find all .log files older than 7 days
ansible.builtin.find:
paths: /var/log/myapp
patterns: "*.log"
age: 7d
register: old_logs
- name: Remove old log files
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ old_logs.files }}"
loop_control:
label: "{{ item.path }}"
See also: Ansible slurp Module: Read Remote Files as Base64 (Complete Guide)
Delete All Files in a Directory (Keep Directory)
- name: Find all files in temp directory
ansible.builtin.find:
paths: /opt/app/tmp
file_type: any
register: temp_contents
- name: Remove all contents
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ temp_contents.files }}"
loop_control:
label: "{{ item.path }}"
Safe Deletion Pattern: Check Before Delete
- name: Check if file exists
ansible.builtin.stat:
path: /opt/app/data.db
register: data_file
- name: Backup before delete
ansible.builtin.copy:
src: /opt/app/data.db
dest: /backup/data.db.{{ ansible_date_time.iso8601_basic_short }}
remote_src: true
when: data_file.stat.exists
- name: Remove original
ansible.builtin.file:
path: /opt/app/data.db
state: absent
when: data_file.stat.exists
Clean Up Old Deployments
- name: Find old deployment directories
ansible.builtin.find:
paths: /opt/app/releases
file_type: directory
age: 30d
register: old_releases
- name: Keep only last 5 releases
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ (old_releases.files | sort(attribute='mtime') | list)[:-5] }}"
loop_control:
label: "{{ item.path | basename }}"
when: old_releases.files | length > 5
Delete Files on Windows
- name: Remove file on Windows
ansible.windows.win_file:
path: C:\Temp\installer.exe
state: absent
- name: Remove directory on Windows
ansible.windows.win_file:
path: C:\Temp\build
state: absent
Common Mistakes
Not Using state: absent
# WRONG — this creates an empty file, doesn't delete!
- ansible.builtin.file:
path: /tmp/old-file.txt
state: touch
# RIGHT — state: absent deletes
- ansible.builtin.file:
path: /tmp/old-file.txt
state: absent
Using command Instead of file Module
# BAD — not idempotent, ansible-lint warns
- ansible.builtin.command: rm -rf /tmp/build
# GOOD — idempotent, clean
- ansible.builtin.file:
path: /tmp/build
state: absent
FAQ
Is state: absent idempotent?
Yes. If the file or directory doesn't exist, Ansible reports ok (no change). If it exists, Ansible removes it and reports changed. Running the task again reports ok.
How do I delete files matching a wildcard pattern?
Use ansible.builtin.find to locate files matching your pattern, register the results, then loop over them with ansible.builtin.file + state: absent. The file module doesn't support glob patterns directly.
Can I recover deleted files?
No. state: absent permanently removes files — there's no recycle bin. Always back up important files before deletion, and test your playbook with --check mode first.
Conclusion
Use ansible.builtin.file with state: absent for all file and directory deletions. It's idempotent, clean, and the recommended approach. For pattern-based deletion, combine find + file modules. Always back up before deleting important data.
Related Articles
• Ansible file Module Guide • Ansible File Operations • Ansible Check If File ExistsCategory: installation