Deleting a file (not unlinking)

4,914 views
Skip to first unread message

robertj

unread,
Nov 14, 2011, 3:21:38 PM11/14/11
to nod...@googlegroups.com
Hi,

I really want to delete a file from the file system instead of just unlinking.
Is there any ideas on how to do this?

unlinking doesnt seem to work - I am pretty sure that no other process is using the file descriptor
as I am creating the file in my test suite and I am "deleting" (trying to unlink it) within the
same test suite.

I am using fs.unlinkSync "filename" on a mac os x and I am using fs.writeFileSync to write contents to the file.

any help is greatly appreciated

Best regards

Robert

robertj

unread,
Nov 14, 2011, 3:32:06 PM11/14/11
to nod...@googlegroups.com
could it be that fs.writeFileSync does not release its file handle?

I can delete the files from another process called after ending the process that created the file via unlinkSync

Axel Kittenberger

unread,
Nov 14, 2011, 3:32:39 PM11/14/11
to nod...@googlegroups.com
On Unix Systems an unlinked file is removed from the harddisk only
when no process has any open filedescriptor handles of it. Likely your
programm still has a handle of it. Close it. Then the space will be
freed.

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

Diogo Resende

unread,
Nov 14, 2011, 7:00:09 PM11/14/11
to nod...@googlegroups.com

node's unlink*() is not the same as php's unlink()

- node: remove link
- php: remove file

---
Diogo R.

Ryan Schmidt

unread,
Nov 14, 2011, 8:03:16 PM11/14/11
to nod...@googlegroups.com

They should be the same, shouldn't they? According to http://nodejs.org/docs/v0.6.1/api/fs.html#fs.unlink node's unlink is "Asynchronous unlink(2)". And according to "man 2 unlink" it "removes the link named by path from its directory and decrements the link count of the file which was referenced by the link. If that decrement reduces the link count of the file to zero, and no process has the file open, then all resources associated with the file are reclaimed." According to http://php.net/unlink php's unlink is "Similar to the Unix C unlink() function". The point is, "unlink" is how you remove a file.


Matt

unread,
Nov 15, 2011, 12:23:05 AM11/15/11
to nod...@googlegroups.com
You are wrong. They are the same.

Diogo Resende

unread,
Nov 15, 2011, 5:34:07 AM11/15/11
to nod...@googlegroups.com
On Tue, 15 Nov 2011 00:23:05 -0500, Matt wrote:

> On Mon, Nov 14, 2011 at 7:00 PM, Diogo Resende wrote:
>
>> On Mon, 14 Nov 2011 12:32:06 -0800 (PST), robertj wrote:
>>
>>> could it be that fs.writeFileSync does not release its file
>>> handle?
>>>
>>> I can delete the files from another process called after ending
>>> the
>>> process that created the file via unlinkSync
>>
>> node's unlink*() is not the same as php's unlink()
>>
>> - node: remove link
>> - php: remove file
>
> You are wrong. They are the same.

I'm sorry, I was really convinced there was an fs.remove()
and fs.unlink() :P

---
Diogo R.

mahboob rahman

unread,
Dec 27, 2013, 2:41:24 AM12/27/13
to nod...@googlegroups.com
I am using node in Windows environment.
When I use fs.unlinkSync(fileName), it seems to work. 
After the unlinkSync statement is executed, if I do a fs.existsSync(filename) it returns false indicating the file does not exists but when I go to the physical drive I could still see the file.
At this point of time if I try and delete the file manually it throws Access denied.
However, the file is automatically removed from the file system only when I stop the executing node script file. 
Is this the expected behavior?


Thanks,
M
Reply all
Reply to author
Forward
0 new messages