# Deleting files based on a name pattern

**URL:** <https://forum.ansible.com/t/deleting-files-based-on-a-name-pattern/14921>\
**Category:** Ansible Project\
**Created:** [April 2, 2014, 5:05pm UTC](https://forum.ansible.com/t/deleting-files-based-on-a-name-pattern/14921 "2014-04-02T17:05:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Carroll](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@James\_Carroll](https://forum.ansible.com/u/James_Carroll)\
**Post date:** [April 2, 2014, 5:05pm UTC](https://forum.ansible.com/t/deleting-files-based-on-a-name-pattern/14921/1 "2014-04-02T17:05:24Z")

</div>

I’m trying to clear a directory with certain file names based on a pattern and it doesn’t seem to work:

- name: Remove any extant jars  
file: name=“{{app\_home}}/server/deploy/{{item}}” state=absent  
with\_items:
- “_token_.jar”
- “_opm_.jar”
- “_pf-sm_.jar”

This isn’t working; the existing jars stay there even though Ansible reports “CHANGED” on all three.

I need this since these jars have their versions in their names and I need to make sure that they are deleted prior to pushing the updated files.

Thanks,

```auto
__________________________________________________________________
The information contained in this email message and any attachment may be privileged, confidential, proprietary or otherwise protected from disclosure. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution, copying or use of this message and any attachment is strictly prohibited. If you have received this message in error, please notify us immediately by replying to the message and permanently delete it from your computer and destroy any printout thereof.

```

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 2, 2014, 8:33pm UTC](https://forum.ansible.com/t/deleting-files-based-on-a-name-pattern/14921/2 "2014-04-02T20:33:19Z")

</div>

Nope, the file module doesn’t accept shell wildcards, it needs real filenames.

I’d shell out to rm in this case, though you won’t be able to use the “deletes=” keyword to the shell module to override the “changed value” so it will register as a change every time.

---

<div class="post-metadata">

**Author:** ![James\_Carroll](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@James\_Carroll](https://forum.ansible.com/u/James_Carroll)\
**Post date:** [April 3, 2014, 3:37pm UTC](https://forum.ansible.com/t/deleting-files-based-on-a-name-pattern/14921/3 "2014-04-03T15:37:55Z")

</div>

Thanks. I kind of came to that same conclusion about shelling it out, but hoping I was missing something.

James

---

<div class="post-metadata">

**Author:** ![David\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@David\_Adams](https://forum.ansible.com/u/David_Adams)\
**Post date:** [April 3, 2014, 5:45pm UTC](https://forum.ansible.com/t/deleting-files-based-on-a-name-pattern/14921/4 "2014-04-03T17:45:01Z")

</div>

You could shell out to collect the filenames if you still want to use the Ansible objects to do the deletions (this code assumes your shell is bash):

- shell: ls {{app\_home}}/server/deploy/_{token,opm,pf-sm}_.jar  
register: jars\_to\_cleanup  
ignore\_errors: yes

- file: name={{item}} state=absent  
with\_items: jars\_to\_cleanup.stdout\_lines

Not sure if that’s better or worse than just shelling out for the rm statement, but it’s an option. Perhaps Ansible needs a glob-expansion function/operator to handle such a case?
