# blockinfile, delete the rest

**URL:** <https://forum.ansible.com/t/blockinfile-delete-the-rest/34891>\
**Category:** Ansible Project\
**Created:** [March 30, 2021, 9:42pm UTC](https://forum.ansible.com/t/blockinfile-delete-the-rest/34891 "2021-03-30T21:42:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![need\_abettername](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@need\_abettername](https://forum.ansible.com/u/need_abettername)\
**Post date:** [March 30, 2021, 9:42pm UTC](https://forum.ansible.com/t/blockinfile-delete-the-rest/34891/1 "2021-03-30T21:42:46Z")

</div>

Hi,

I’d like to edit a file using block in file and delete anything in the file that isn’t in that block, in an idempotent way i,e, not just deleting the file first.

I suspect I should be using a template here but partly want to scratch the itch of whether it’s possible, is there a reasonable way?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 31, 2021, 4:44am UTC](https://forum.ansible.com/t/blockinfile-delete-the-rest/34891/2 "2021-03-31T04:44:33Z")

</div>

> Hi,
> 
> I’d like to edit a file using block in file and delete anything in the file that isn’t in that block, in an idempotent way i,e, not just deleting the file first.

This would have exactly the same results as using the template module, only harder to do.

> I suspect I should be using a template here but partly want to scratch the itch of whether it’s possible, is there a reasonable way?

You probably should. Put differently, why would you not use the template module?
