# Equivalent of rm -Rf deleting subfolders

**URL:** <https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481>\
**Category:** Get Help\
**Tags:** debian\
**Created:** [March 4, 2026, 2:57pm UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481 "2026-03-04T14:57:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cndpansible](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@cndpansible](https://forum.ansible.com/u/cndpansible)\
**Post date:** [March 4, 2026, 2:57pm UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/1 "2026-03-04T14:57:50Z")

</div>

Hi All,

I’m proudly getting used to my first Ansible playbooks, but I still have an issue with deletion of a folder content together with all subfolders.

I need the equivalent of

```auto
rm -Rf /home/guest/*
rm -Rf /home/guest/.*

```

and my playbook deletes files (including hidden ones) but not subfolders

```auto
---
- name: Empty a dir
  hosts: test
  tasks:
    - name: Find content of /home/guest/
      ansible.builtin.find:
        paths: /home/guest/
        hidden: yes
        recurse: yes
      register: contenu_rep
    - name: Delete found content
      ansible.builtin.file:
        path: "{{ item.path }}"
        state: absent
      loop: "{{ contenu_rep.files }}"

```

I’ve read about deleting the folder itself and then re-create it, but I must keep my folder (creation times, ownership, permissions, etc).

What am I missing to delete the whole content of `/home/guest/` , including subfolders, but keep `/home/guest` itself?

Shouldn’t it work with the `recurse` ?

Thank you!

PS: I’d like to avoid just alling a `rm` script, both for my own learning of Ansible, and to benefit from error handling at Ansible level.

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [March 4, 2026, 4:01pm UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/2 "2026-03-04T16:01:02Z")

</div>

Hi @cndpansible, after poking around with this a little, think I was able to make it behave the way you’re asking for by adding `file_type: any` to the `ansible.builtin.find` block as per [the module documentation](https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/find_module.html#parameter-file_type).

During my initial testing, and confirmed by my reading of that page, `ansible.builtin.find` defaults to finding _files_. That, combined with `recurse: true` was finding files at all levels, but not the directories.

I think that explains why it was behaving that way.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [March 4, 2026, 4:41pm UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/3 "2026-03-04T16:41:13Z")

</div>

Doing it the ansible way _will_ work, but if your folder contains many items then it will get slow very quickly. In practice for me this only worked OK up to a few hundred files.  
For anything more I used `ansible.builtin.command` and `rm -rf`…

---

<div class="post-metadata">

**Author:** ![cndpansible](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@cndpansible](https://forum.ansible.com/u/cndpansible)\
**Post date:** [March 5, 2026, 10:49am UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/4 "2026-03-05T10:49:55Z")

</div>

Hi All,  
Thanks a lot @Lyle_McKarns and @dnmvisser

Indeed, Ansible does not delete directories, but it empties them fine.  
This is perfectly OK to me, I will work to understand the solutions but might stick to the original syntax.

Thanks as well for the warning about timing. I tested and indeed, emptying ~/.cache (even if not that big) takes a long time 😄

---

<div class="post-metadata">

**Author:** ![U880D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/u880d/32/10032_2.png) [@U880D](https://forum.ansible.com/u/U880D)\
**Post date:** [March 5, 2026, 11:14am UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/5 "2026-03-05T11:14:02Z")

</div>

> [@cndpansible](#):
>
> I’ve read about deleting the folder itself and then re-create it, but I must keep my folder (creation times, ownership, permissions, etc).

Since an approach like

```yaml
- name: Delete all content (including directory)
  ansible.builtin.file:
    path: /home/guest/
	state: absent

```

is not feasible, you could take advantage of [Developing modules](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html) and [Create a custom module](https://docs.ansible.com/projects/ansible/latest/dev_guide/developing_modules_general.html#creating-a-standalone-module). Find a prototype here:

**Custom Module `library/rm_r.py`**

```python
#!/usr/bin/python

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.module_utils.basic import AnsibleModule

import os, shutil

def run_module():
    module_args = dict(
        path=dict(type='str', required=True)
    )

    result = dict(
        changed=False,
        removed=''
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
    )

    if module.check_mode:
        module.exit_json(**result)

    folder = module.params['path']

    # Main logic

    for f in os.listdir(folder):
        full_path = os.path.join(folder, f)
        if os.path.isfile(full_path) or os.path.islink(full_path):
            os.unlink(full_path)
        elif os.path.isdir(full_path):
            shutil.rmtree(full_path)
        result['removed'] = result['removed'] + full_path + '\n'
        result['changed'] = True

    module.exit_json(**result)

def main():
    run_module()

if __name__ == ' __main__':
    main()

```

**Playbook `rm_r.yml`**

```yaml
---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Delete directory content
    rm_r:
      path: "/home/guest/foo/"
    register: result

  - debug:
      msg: "{{ result }}"

```

For a simple test

```bash
tree foo/
foo/
├── bar
├── one.tst
└── two.tst

1 directory, 2 files

```

it will result into an output of

```yaml
TASK [debug] ************************
ok: [localhost] =>
  msg:
    changed: true
    failed: false
    removed: |-
      /home/guest/foo/bar
      /home/guest/foo/one.tst
      /home/guest/foo/two.tst
	  
Delete directory content ----- 0.38s
debug ------------------------ 0.03s

```

deleted files and folder, and leave an empty directory

```bash
tree foo/
foo/

0 directories, 0 files

```

Just add error handling, enhance the behavior and perform further tests as necessary.

---

<div class="post-metadata">

**Author:** ![U880D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/u880d/32/10032_2.png) [@U880D](https://forum.ansible.com/u/U880D)\
**Post date:** [March 5, 2026, 11:21am UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/6 "2026-03-05T11:21:41Z")

</div>

> [@dnmvisser](#):
>
> if your folder contains many items then it will get slow very quickly

The in [my other answer](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/5) given approach with a Custom Module will have significant performance adavantages

> [@dnmvisser](#):
>
> Doing it the ansible way _will_ work

and it is the recommended Ansible way for such Use Cases.

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [March 5, 2026, 2:16pm UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/7 "2026-03-05T14:16:41Z")

</div>

I did a little more digging, and if what you’re really after is “delete everything from this directory” without taking a huge performance hit like like @dnmvisser mentioned, you can simply add `file_type: any` and remove `recurse` (and keep `hidden: true`). This will return all files, and all top level directories.

And `ansible.builtin.file` will delete a directory _and its content_ with `state: absent`

---

<div class="post-metadata">

**Author:** ![cndpansible](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@cndpansible](https://forum.ansible.com/u/cndpansible)\
**Post date:** [March 5, 2026, 5:31pm UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/8 "2026-03-05T17:31:33Z")

</div>

Great, I understand, covering all “file types” will include directories, and removing them at top level (no recursion needed) won’t impact performance, correct?

Thanks a lot!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 4, 2026, 5:32pm UTC](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481/9 "2026-04-04T17:32:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
