# Unsetting a set\_fact variable

**URL:** <https://forum.ansible.com/t/unsetting-a-set-fact-variable/19740>\
**Category:** Ansible Project\
**Created:** [November 12, 2015, 4:16am UTC](https://forum.ansible.com/t/unsetting-a-set-fact-variable/19740 "2015-11-12T04:16:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![K\_Cheng](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@K\_Cheng](https://forum.ansible.com/u/K_Cheng)\
**Post date:** [November 12, 2015, 4:16am UTC](https://forum.ansible.com/t/unsetting-a-set-fact-variable/19740/1 "2015-11-12T04:16:28Z")

</div>

Hi guys,

I’ve got a setup which sets a fact based on whether a file exists or not.

This fact works for one run in the play but doesn’t work when I want that value to be removed for another task inside the play.

> - name: Check if a bucket\_policy file exists for {{ bucket\_name.name }}
> 
> stat:
> 
> path: “{{ role\_path }}/files/{{ bucket\_name.name }}.json”
> 
> register: bucket\_policy\_status
> 
> - name: Read in bucket\_policy if policy\_file exists for {{ bucket\_name.name }}
> 
> set\_fact:
> 
> bucket\_policy\_json: “{{ lookup(‘file’, bucket\_name.name + ‘.json’) }}”
> 
> when: bucket\_policy\_status.stat.exists
> 
> - name: Set policy variable to None when bucket policy doesn’t exist for {{ bucket\_name.name }}
> 
> set\_fact:
> 
> bucket\_policy\_json: “{{ omit }}”
> 
> when: not bucket\_policy\_status.stat.exists
> 
> - name: Create S3 bucket {{ bucket\_name.name }}
> 
> s3\_bucket:
> 
> name: “{{ item }}”
> 
> state: present
> 
> policy: “{{ bucket\_policy\_json | default(omit) }}”
> 
> profile: “{{ aws\_account\_name }}”
> 
> region: “{{ bucket\_name.region }}”
> 
> with\_items: “{{ bucket\_name.name | default({}) }}”
> 
> register: created\_bucket

Is there a way to “unset” a set\_fact to something that default(omit) can recognise?

At the moment, if the first run through sets bucket\_policy\_json to a value, the next thing that comes along that hits the when: not bucket\_policy\_status.stat.exists statement does not successfully clear that fact to an undefined state.

Is there a magic variable to “undefine” a fact by any chance?

Regards

Karen

---

<div class="post-metadata">

**Author:** ![K\_Cheng](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@K\_Cheng](https://forum.ansible.com/u/K_Cheng)\
**Post date:** [November 12, 2015, 4:37am UTC](https://forum.ansible.com/t/unsetting-a-set-fact-variable/19740/2 "2015-11-12T04:37:15Z")

</div>

Pretty sure there isn’t an unset function for set\_fact. Ended up going with a ternary jinja2 filter instead which appears to work:
