# 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:** 1\
**Showing post:** 5

<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.

---

_[View the full topic](https://forum.ansible.com/t/equivalent-of-rm-rf-deleting-subfolders/45481)._
