AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 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 "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, 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 Files in Directory: find & file Modules (Examples) — Video Tutorial
How to delete only files inside a directory in Ansible using find and file modules. Clean up logs, temp files, and old backups by age, pattern, and size.
What You'll Learn
- How to Delete Only Files Inside a Directory with Ansible?
- Ansible How to Delete Only Files Inside a Directory
- Parameters
- Links
- code
- execution
- before execution
- after execution
- Conclusion
- Delete All Files in Directory
Full Tutorial Content
How to Delete Only Files Inside a Directory with Ansible?
I'm going to show you a live Playbook with some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible How to Delete Only Files Inside a Directory
- `ansible.builtin.find`
- Return a list of files based on specific criteria
Today we're talking about the Ansible module `find`.
The full name is `ansible.builtin.find`, which means that is part of the collection included in the `ansible-core` builtin collection.
This module returns a list of files based on specific criteria using the `find` popular Unix command.
Parameters
- paths string - List of paths of directories to search
- hidden boolean - no/yes
- recurse boolean - recursively descend into the directory looking for files
- file_type string - file/directory/any/link
The most important parameters of the `find` module for this use case.
The mandatory parameter `paths` specify the list of paths of directories to search.
You could include hidden files with the `hidden` parameter.
As well as recurse in any directory under the main path with the `recurse` parameter.
Another useful parameter is `file_type`, defaults to `file` but you could filter for `directory`, `link` or `any` filesystem object type.
Links
- [ansible.builtin.find](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html)
- [ansible.builtin.file](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html)
## Playbook
How to delete Ooly files inside a directory with Ansible Playbook.
I'm going to delete only the files and directories under the example folder of my login user (`devops`).
code
```yaml
---
- name: delete file(s) in dir Playbook
hosts: all
vars:
mypath: "/home/devops/example"
tasks:
- name: collect files
ansible.builtin.find:
paths: "{{ mypath }}"
hidden: true
recurse: true
file_type: any
register: collected_files
- name: remove collected files
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
with_items: "{{ collected_files }}"
```
execution
```bash
$ ansible-playbook -i virtualmachines/demo/inventory delete\ file\ or\ directory/file_onlyfiles.yml
PLAY [delete file(s) in dir Playbook] *****************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [collect files] ******************************************************************************
ok: [demo.example.com]
TASK [remove collected files and directories] *****************************************************
changed: [demo.example.com] => (item={'path': '/home/devops/example/file1.txt', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1001, 'gid': 10, 'size': 0, 'inode': 484310, 'dev': 6476
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 7 min
- Category: troubleshooting
Read the full written article: Ansible Delete Files in Directory: find & file Modules (Examples)
Related Video Tutorials
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
- Backup With Robocopy on Windows - Ansible module win_robocopy — How to automate the backup synchronization of an “examples” folder to “examples-backup” on Windows using RoboCopy with Ansible module win_robocopy.
- Ansible Create Directory: file Module with state=directory Guide — How to create directories with Ansible file module. Set permissions, ownership, create recursive paths, manage directory structures with examples.
- Create Directories in Windows Systems Using Ansible — Master directory creation on Windows systems with Ansible’s win_file module. Follow our live Playbook example for efficient directory management.
- Ansible Delete Files & Directories: file Module state=absent — How to delete files and directories with Ansible file module. Remove files, directories, symlinks, and manage cleanup tasks with examples.
- Ansible unarchive Module: Extract tar, zip & gz Archives (ansible.builtin.unarchive) — Complete guide to ansible.builtin.unarchive module. Extract tar.gz, zip, and bz2 archives on remote hosts with examples, creates parameter, and remote_src.