# Find what has locked/held a file

**URL:** <https://discourse.julialang.org/t/find-what-has-locked-held-a-file/23278>\
**Category:** General Usage\
**Created:** [April 18, 2019, 1:38pm UTC](https://discourse.julialang.org/t/find-what-has-locked-held-a-file/23278 "2019-04-18T13:38:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremy/32/295_2.png) [@Jeremy](https://discourse.julialang.org/u/Jeremy)\
**Post date:** [April 18, 2019, 1:38pm UTC](https://discourse.julialang.org/t/find-what-has-locked-held-a-file/23278/1 "2019-04-18T13:38:32Z")

</div>

Is there a good way of finding out what has locked a file or what is holding it so that it can’t be deleted?

In my code I unzip a few to a temporary folder, read the header lines with `readline`, and then read the bulk of the file with `CSV.read`. Afterwards I delete the file, or at least try to but in Windows always get the error `IOError: unlink: resource busy or locked (EBUSY)`. I do `close` the file when I’m done reading from it.

Once my code errors out I can delete the file with `rm` in the REPL. I’m assuming when the function returns all the open files are closed. Maybe I should put the reading part in a separate function so ensure that everything is closed? That feels like a hack though.

Windows 10, Julia 0.7, Pkg.test(“[ClimateDataIO](https://github.com/JeremyRueffer/ClimateDataIO.jl)”) specifically lines 286-292 of runtests.jl which tests ghg\_read.jl.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [April 19, 2019, 10:21am UTC](https://discourse.julialang.org/t/find-what-has-locked-held-a-file/23278/2 "2019-04-19T10:21:38Z")

</div>

This doesn’t address the issue of “who’s causing it?” but you might consider `GC.gc()` before trying to delete the file. If the file is opened with `Mmap.mmap`, then you wouldn’t be able to delete the file until any temporaries have been garbage-collected.

EDIT: if the variable is still local you might have to redefine it before this works. E.g., if `x` comes from a `mmap`ped file, you might have to say `x = 0; GC.gc()`.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [April 19, 2019, 1:00pm UTC](https://discourse.julialang.org/t/find-what-has-locked-held-a-file/23278/3 "2019-04-19T13:00:18Z")

</div>

Try this, or the following (Process Explorer, but that’s a separate download):

[https://superuser.com/a/643312](https://superuser.com/a/643312)

---

<div class="post-metadata">

**Author:** ![Jeremy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremy/32/295_2.png) [@Jeremy](https://discourse.julialang.org/u/Jeremy)\
**Post date:** [April 29, 2019, 12:10pm UTC](https://discourse.julialang.org/t/find-what-has-locked-held-a-file/23278/4 "2019-04-29T12:10:18Z")

</div>

Finally had a chance to test it out and it works! I had to place it soon after the `CSV.read` commands and it now works in Windows.

@ihnorton, cool, I didn’t know you could do that. I don’t think it would have helped in this case but it’s good to know.
