# Delete all files with a specific extension in a particular directory

**URL:** <https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 4, 2020, 9:05pm UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764 "2020-05-04T21:05:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [May 4, 2020, 9:05pm UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764/1 "2020-05-04T21:05:51Z")

</div>

A specific directory, say

```
"C:/Users/me/data/"

```

contains many files. Plenty of these files have the same extension, say .txt.

What code will delete all files in the stated directory with a .txt extension, but leave all others.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 4, 2020, 9:12pm UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764/2 "2020-05-04T21:12:08Z")

</div>

Have you checked the “Filesystem” commands: [Filesystem · The Julia Language](https://docs.julialang.org/en/v1/base/file/)?

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [May 4, 2020, 9:18pm UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764/3 "2020-05-04T21:18:06Z")

</div>

I did see see “rm” and “mkpath”. And deleting a single file from a specified directory looked simple enough, but specifying all files in that directory with a specific extensions for deletion, was not clear to me.

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [May 4, 2020, 9:22pm UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764/4 "2020-05-04T21:22:04Z")

</div>

You could do something like: `filter(x->endswith(x, ".txt"), readdir(dir))` to get a list of all the “\*.txt” files in the directory (assuming you don’t have folders in the directory that end with “.txt”).

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 4, 2020, 9:25pm UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764/5 "2020-05-04T21:25:30Z")

</div>

Deleting all files that match a pattern is really more of a shell operation. All(?) programming languages I’ve worked with give you a command to delete a single, and leave it up the programmer to delete each file individually.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 4, 2020, 9:45pm UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764/6 "2020-05-04T21:45:34Z")

</div>

This doesn’t work yet, but in version 1.5 you can write

```julia
foreach(rm, filter(endswith(".txt"), readdir())) # untested

```

which is becoming quite concise.

---

<div class="post-metadata">

**Author:** ![Arpit\_Babbar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arpit_babbar/32/220811_2.png) [@Arpit\_Babbar](https://discourse.julialang.org/u/Arpit_Babbar)\
**Post date:** [July 22, 2021, 6:30am UTC](https://discourse.julialang.org/t/delete-all-files-with-a-specific-extension-in-a-particular-directory/38764/7 "2021-07-22T06:30:11Z")

</div>

That works!

To delete all text files in a particular directory, it slightly elongates to

```julia
foreach(rm, filter(endswith(".txt"), readdir(dir,join=true)))

```
