AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Delete File: Remove Files & Directories (state=absent Guide) — Video Tutorial

How to delete files and directories in Ansible with the file module (state=absent). Remove files, recursive directory deletion, check before delete.

Watch Video

Watch "Ansible Delete File: Remove Files & Directories (state=absent Guide)" on YouTube

What You'll Learn

Full Tutorial Content

How to Delete files or directory with Ansible? I'm going to show you a live Playbook and some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot. Ansible delete file/directory Today we're talking about the Ansible module file. The full name is `ansible.builtin.file`, which means that is part of the collection of modules "builtin" with ansible and shipped with it. It's a module pretty stable and out for years. It works in a different variety of operating systems. It manages files and file properties. For Windows targets, use the `ansible.windows.win_file` module instead. Main Parameters - path _string_ (dest, name) - file path - owner _string_ - user - group _string_ - group - mode _raw_ - Ex: '0644' or 'u=rw,g=r,o=r' - state _string_ - file/absent/directory/hard/link/touch - setype/seuser/selevel - SELinux This module has some parameters to perform any tasks. The only required is "path", where you specify the filesystem path of the file you're going to edit. The parameter "owner" sets the user that should own the file/directory. The parameter "group" sets the group that should own the file/directory. The parameter "mode" sets the permissions in the UNIX way of the file/directory. The state defines the type of object we are modifying, the default is "file" but we could also handle directories, hardlink, symlink, or only update the access time with the "touch" option. For our use case, we are going to use the "absent" option. Let me also highlight that we could also specify the SELinux properties. ## Playbook Let's jump into a real-life playbook on how to delete files or directories with Ansible. - file.yml ```yaml --- - name: file module demo hosts: all vars: mypath: "/home/devops/deleteme" become: false tasks: - name: "{{ mypath }}" not present ansible.builtin.file: path: "{{ mypath }}" state: "absent" ``` [code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/delete%20file%20or%20directory) Conclusion Now you know better the Ansible module win_chocolatey and how you could use it successfully in your playbook to automate your day-to-day activities. Advanced Deletion Examples Delete with wildcard (using find module) The `file` module doesn't support wildcards. Use `find` first: ```yaml - name: Find all .tmp files ansible.builtin.find: paths: /tmp patterns: "*.tmp" age: "7d" # Only files older than 7 days register: old_files - name: Delete old .tmp files ansible.builtin.file: path: "{{ item.path }}" state: absent loop: "{{ old_files.files }}" ``` Delete multiple paths ```yaml - name: Clean up deployment artifacts ansible.builtin.file: path: "{{ item }}" state: absent loop: - /tmp/build-cache - /tmp/deploy-staging - /var/log/myapp/old-logs become: true ``` Safe deletion with confirmation ```yaml - name: Check what we're about to delete ansible.builtin.stat: path: "{{ del

About This Tutorial

Read the full written article: Ansible Delete File: Remove Files & Directories (state=absent Guide)

Topics Covered

Related Video Tutorials