# \[how can i delete a file system..please help\]

**URL:** <https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912>\
**Category:** Rails\
**Created:** [June 9, 2006, 11:52am UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912 "2006-06-09T11:52:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 11:52am UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/1 "2006-06-09T11:52:55Z")

</div>

Hi,  
I’m trying to delete a file system (\<xml\_26548975.xml\>)  
File.delete(“xml\_26548975.xml”)  
But I get this error:  
“Permission denied - ./script/…/config/…/uploads/xml\_26548975.xml”.  
Why?

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/2 "2006-06-09T17:12:53Z")

</div>

What OS are you using for your server?

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/3 "2006-06-09T16:12:13Z")

</div>

well, from the error message, it looks like you are not allowed to  
delete  
the file due to permissions. i would start there.

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/4 "2006-06-09T17:45:37Z")

</div>

I’m just uploading a file - copying the content to another file, which  
afterwards I want to delete it/unlink it/get lost.

uploaded\_file = RAILS\_ROOT + “/uploads/xml\_” + generate\_code + “.xml”  
File.open(uploaded\_file, “wb”) do |f|  
f.write(@file.read)

after some time…File.delete("…").

I’m using XP and WEBrick( I think he’s to blame).

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/5 "2006-06-09T21:24:02Z")

</div>

Ioana K. wrote:

> uploaded\_file = RAILS\_ROOT + “/uploads/xml\_” + generate\_code + “.xml”
> 
> ```
> File.open(uploaded_file, "wb") do |f|
> f.write(@file.read)
> 
> ```
> 
> after some time…File.delete("…").

FileUtils.rm “#{uploaded\_file}” should do what you’re looking for,  
although  
you may need to either stip off RAIL\_ROOT or save the path without  
RAILS\_ROOT prior to the File.open. That is, I think FileUtils assumes  
it’s  
working from RAILS\_ROOT whereas File does not.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/6 "2006-06-09T19:10:53Z")

</div>

On 6/9/06, Ioana K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> I’m trying to delete a file system (\<xml\_26548975.xml\>)

A “file system”? xml\_26548975.xml is a regular file, right? Since if  
it’s a directory tree or something, you’re not using the correct  
method (should look at Dir.delete instead).

If it is a file, the first thing I would check is if you’re trying to  
File.delete it while it’s still open. The code below shows you’re  
using a block scope for the output file, which is good since it takes  
care of closing the file for you. However, looks like you’re not using  
the same good pattern for the input file (@file). You can, and should  
consider it, to avoid bugs like the one that might be haunting you  
right now (there’s no problem nesting File.open blocks if that’s what  
hindered you).

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/7 "2006-06-09T21:30:31Z")

</div>

On 6/9/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Why would FileUtils assume its working from RAILS\_ROOT? It’s a Ruby  
library, not a Rails’ one. Does Ruby modify it in some way? (sounds a  
bit unlikely…)

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 10:28pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/8 "2006-06-09T22:28:26Z")

</div>

On 6/9/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > working from RAILS\_ROOT whereas File does not.
> 
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Check what’s the CWD (with Dir.pwd) in each phase. My guess is they  
would be different - RAILS\_ROOT for the #rm call, somehing else for  
the #copy.

The CWD is dependent upon your stack setup. For example, with WEBrick  
it’s often RAILS\_DIR throughout the app, while with FastCGI and  
Lighttpd it’s /public. What’s your setup?

---

<div class="post-metadata">

**Author:** ![Ioana\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/90ced4/32.png) [@Ioana\_K](https://www.ruby-forum.com/u/Ioana_K)\
**Post date:** [June 9, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/how-can-i-delete-a-file-system-please-help/62912/9 "2006-06-09T21:59:12Z")

</div>

Hi Alder,

Alder G. wrote:

> Why would FileUtils assume its working from RAILS\_ROOT? It’s a Ruby  
> library, not a Rails’ one. Does Ruby modify it in some way? (sounds a  
> bit unlikely…)

I don’t know. I’m just going on my recent experience. I’m doing a  
little  
app that includes a file upload. After looking back at that code more  
closely, I’m just more confused. FileUtils.copy seems to need  
RAILS\_ROOT  
but FileUtils.rm does not. Here’s the file upload code I’m using.

@filenametouse = “temp-readin” + nextfile.id.to\_s + “.xml”

if params[:file\_to\_upload].instance\_of?(Tempfile)  
FileUtils.copy(params[:file\_to\_upload].local\_path,  
“#{RAILS\_ROOT}/public/#{@filenametouse}”)  
else  
File.open("#{RAILS\_ROOT}/public/#{@filenametouse}",“w”){|f|  
f.write(params[:file\_to\_upload].read)  
f.close}  
end

I store the value of @filenametouse in a db record and later retrieve it  
to  
delete the file which I do with this code. (emrec.id just identifies  
the  
group of files I created in a session)

files = Tempfilerec.find(:all,  
:conditions =\> [“emrec\_id = ?”,  
emrec.id])  
files.each {|file|  
FileUtils.rm “public/#{file.filename}”  
}

I remember being confused when I got the code working but, under  
deadline  
pressure, just moved on with a note-to-self to come back later and  
figure  
out “why the difference?” Any ideas?

Best regards,  
Bill
