# Delete files resulting from a search

**URL:** <https://forum.ansible.com/t/delete-files-resulting-from-a-search/5221>\
**Category:** Get Help\
**Created:** [April 30, 2024, 2:47pm UTC](https://forum.ansible.com/t/delete-files-resulting-from-a-search/5221 "2024-04-30T14:47:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![microlinux](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/microlinux/32/2968_2.png) [@microlinux](https://forum.ansible.com/u/microlinux)\
**Post date:** [April 30, 2024, 2:47pm UTC](https://forum.ansible.com/t/delete-files-resulting-from-a-search/5221/1 "2024-04-30T14:47:03Z")

</div>

Hi,

I have yet to wrap my head around the way Ansible does some things.

I have the following task using the `find` module:

```auto
- name: Find existing desktop menu files on target system
  ansible.builtin.find:
    paths: /var/lib/flatpak/app
    recurse: true
    patterns: 'org.darktable.Darktable.desktop'
  register: find_result

```

I included a little debug task to see what this returns:

```auto
- name: Show results
  ansible.builtin.debug:
    var: find_result

```

How can I tell Ansible to simply delete the resulting files ? (Adding a rule like “if there are any” to avoid errors when repeating the play?)

Thanks & cheers !

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [May 1, 2024, 7:39pm UTC](https://forum.ansible.com/t/delete-files-resulting-from-a-search/5221/2 "2024-05-01T19:39:58Z")

</div>

`ansible.builtin.file` module, use parameter `state` with value of `absent`:  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file\_module.html#parameters](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html#parameters)

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 1, 2024, 10:10pm UTC](https://forum.ansible.com/t/delete-files-resulting-from-a-search/5221/3 "2024-05-01T22:10:34Z")

</div>

I created a role for this [GitHub - bcoca/ansible-tidy: tidy role](https://github.com/bcoca/ansible-tidy)

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [May 2, 2024, 11:32am UTC](https://forum.ansible.com/t/delete-files-resulting-from-a-search/5221/4 "2024-05-02T11:32:29Z")

</div>

I use `ansible.builtin.find` and `ansible.builtin.file` for this, there is [an example here](https://git.coop/webarch/icinga/-/blob/master/tasks/master_agents.yml?ref_type=heads#L60), you could try something like this:

```yaml
- name: Delete files found
  ansible.builtin.file:
    path: "{{ file.path }}"
    state: absent
  loop: "{{ find_result.files }}"
  loop_control:
    loop_var: file
  when: find_results.matched > 0

```

To debug the files to delete you could use:

```yaml
- name: Debug files found
  ansible.builtin.debug:
    var: file.path
  loop: "{{ find_result.files }}"
  loop_control:
    loop_var: file
  when: find_results.matched > 0

```
