# Error deleting file

**URL:** <https://discourse.julialang.org/t/error-deleting-file/7787>\
**Category:** General Usage\
**Created:** [December 15, 2017, 1:06pm UTC](https://discourse.julialang.org/t/error-deleting-file/7787 "2017-12-15T13:06:10Z")\
**Posts on this page:** 7\
**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:** [December 15, 2017, 1:06pm UTC](https://discourse.julialang.org/t/error-deleting-file/7787/1 "2017-12-15T13:06:10Z")

</div>

My code unzips a data file, loads it, and deletes the temporary file. It has worked fine in the past but now it fails on the line that deletes the file. It happens in a module I’m working on, [ClimateDataIO.jl](https://github.com/JeremyRueffer/ClimateDataIO.jl) in [ghg\_read.jl (line 89)](https://github.com/JeremyRueffer/ClimateDataIO.jl/blob/0f2da0ecad3c393073bec547b8f14087bccd465d/src/ghg_read.jl#L89). The recent change was switching between `DataFrames.readtable` to `CSV.read`.

The error can be reproduced on Windows machines by running the module tests, `Pkg.test("ClimateDataIO")`.

```julia
rm(temp)

ERROR: LoadError: unlink: operation not permitted (EPERM)
Stacktrace:
 [1] uv_error at .\libuv.jl:68 [inlined]
 [2] unlink(::String) at .\file.jl:500
 [3] #rm#7(::Bool, ::Bool, ::Function, ::String) at .\file.jl:145
 [4] #ghg_read#4(::Bool, ::Function, ::String) at C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\src\ghg_read.jl:89
 [5] (::ClimateDataIO.#kw##ghg_read)(::Array{Any,1}, ::ClimateDataIO.#ghg_read, ::String) at .\<missing>:0
 [6] include_from_node1(::String) at .\loading.jl:569
 [7] include(::String) at .\sysimg.jl:14
 [8] process_options(::Base.JLOptions) at .\client.jl:305
 [9] _start() at .\client.jl:371
while loading C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\test\runtests.jl, in expression starting on line 275

```

It’s not a problem in Linux/Mac but I’ve confirmed the problem on two different Windows machines. It runs successfully in Coveralls and Codecov.

Calling `rm` on the same file in REPL works fine. My guess was that the file wasn’t closing properly until the test failed. The code looks fine, as far as I can tell I’ve closed the file every time I’ve opened it. What is wrong here?

---

<div class="post-metadata">

**Author:** ![apieum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apieum/32/2928_2.png) [@apieum](https://discourse.julialang.org/u/apieum)\
**Post date:** [December 15, 2017, 1:55pm UTC](https://discourse.julialang.org/t/error-deleting-file/7787/2 "2017-12-15T13:55:42Z")

</div>

Hello, `rm(temp)` == `unlink(temp)` (after changing file rights)  
It calls directly libuv C code which is responsible for generating your error message.

You can find lot of similar error for nodejs and python, it’s often for hidden or **temporary directories**.

One corresponding issue is here: [https://github.com/libuv/libuv/pull/316](https://github.com/libuv/libuv/pull/316)  
Another is here:[https://github.com/libuv/libuv/pull/729](https://github.com/libuv/libuv/pull/729)

I guess your problem is using a temporary directory on windows.

I saw an issue on julia github to update libuv version, it may fix it.

---

<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:** [December 15, 2017, 3:07pm UTC](https://discourse.julialang.org/t/error-deleting-file/7787/3 "2017-12-15T15:07:20Z")

</div>

I tried it on `ENV["userprofile"]` and `joinpath(ENV["userprofile"],"Desktop")` and well and neither worked.

Shouldn’t the problem occur at the REPL as well if this is the problem?

---

<div class="post-metadata">

**Author:** ![apieum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apieum/32/2928_2.png) [@apieum](https://discourse.julialang.org/u/apieum)\
**Post date:** [December 15, 2017, 4:28pm UTC](https://discourse.julialang.org/t/error-deleting-file/7787/4 "2017-12-15T16:28:50Z")

</div>

Sorry I don’t know and don’t have window to make tests.  
Is a different approach consisting in creating a temporary directory to do your job and then deleting it, may be acceptable ?  
like line 30:  
replacing : `temp_dir = tempdir()` with `temp_dir = mktempdir()`  
and replacing lines 89:93 with:  
`rm(temp_dir, true; recursive=true)`

---

<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:** [December 18, 2017, 8:45am UTC](https://discourse.julialang.org/t/error-deleting-file/7787/5 "2017-12-18T08:45:16Z")

</div>

I gave it a shot but it didn’t work either. Well worth trying though, thanks for the idea.

---

<div class="post-metadata">

**Author:** ![apieum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apieum/32/2928_2.png) [@apieum](https://discourse.julialang.org/u/apieum)\
**Post date:** [December 19, 2017, 1:54pm UTC](https://discourse.julialang.org/t/error-deleting-file/7787/6 "2017-12-19T13:54:26Z")

</div>

What’s the error message when you remove the temporary directory ? it should be different.

---

<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:** [December 19, 2017, 3:57pm UTC](https://discourse.julialang.org/t/error-deleting-file/7787/7 "2017-12-19T15:57:13Z")

</div>

It looks the same to me. Line 91 is where the `rm` command is.

```julia
ERROR: LoadError: unlink: operation not permitted (EPERM)
Stacktrace:
 [1] uv_error at .\libuv.jl:68 [inlined]
 [2] unlink(::String) at .\file.jl:500
 [3] #rm#7(::Bool, ::Bool, ::Function, ::String) at .\file.jl:145
 [4] (::Base.Filesystem.#kw##rm)(::Array{Any,1}, ::Base.Filesystem.#rm, ::String) at .\<missing>:0
 [5] #rm#7(::Bool, ::Bool, ::Function, ::String) at .\file.jl:155
 [6] (::Base.Filesystem.#kw##rm)(::Array{Any,1}, ::Base.Filesystem.#rm, ::String) at .\<missing>:0
 [7] #ghg_read#4(::Bool, ::Function, ::String) at C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\src\ghg_read.jl:91
 [8] (::ClimateDataIO.#kw##ghg_read)(::Array{Any,1}, ::ClimateDataIO.#ghg_read, ::String) at .\<missing>:0
 [9] include_from_node1(::String) at .\loading.jl:576
 [10] include(::String) at .\sysimg.jl:14
 [11] process_options(::Base.JLOptions) at .\client.jl:305
 [12] _start() at .\client.jl:371
while loading C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\test\runtests.jl, in expression starting on line 275

```

This command removes the folder without errors. It’s the same usage in the module.

```julia
rm("C:\\Users\\JohnDoe\\AppData\\Local\\Temp\\jl_73C.tmp",recursive=true)

```

Julia 0.6.2 reacts the same.
