fs.rm() and/or fs.rmdir(path, recursive)?

713 views
Skip to first unread message

Ryan Gahl

unread,
Jun 18, 2010, 10:34:12 AM6/18/10
to nod...@googlegroups.com
I had a need to delete a directory that wasn't empty, and had to resort to child_process.exec("rm -r " + dirName). So that leads me to the main Q: why is there no fs.rm() and fs.rmRecursive() or the like? How are people deleting files? Secondarily, assuming fs.rm() were implemented, would a patch be welcome to fs.rmdir() that enabled a bool param to make it delete recursively (which would of course use).

I'm not sure what the best implementation would be, but in general am just wondering what gives with file deletion... thanks.

-rg

Ryan Gahl

unread,
Jun 18, 2010, 10:53:06 AM6/18/10
to nod...@googlegroups.com
...found .unlink via help from an IRC dude... but am still curious about the recursive case. 

Fabian Jakobs

unread,
Jun 18, 2010, 11:24:31 AM6/18/10
to nod...@googlegroups.com
Hi,

I have implemented a sync and async version of rmTree. See this gist
<http://gist.github.com/443774>. If there is interest I can prepare a
patch for node's fs module.

Best Fabian

> --
> 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.
>

Ryan Gahl

unread,
Jun 18, 2010, 11:48:35 AM6/18/10
to nod...@googlegroups.com
+1 interest.

Fabian Jakobs

unread,
Jun 18, 2010, 11:52:43 AM6/18/10
to nod...@googlegroups.com
OK, I'll prepare something next week.

Aaron Heckmann

unread,
Jun 18, 2010, 11:57:09 AM6/18/10
to nod...@googlegroups.com
I don't have the link handy but I believe isaacs has a rm -rf module
in npm somewhere.


--
Aaron
http://clickdummy.net

Isaac Schlueter

unread,
Jun 18, 2010, 1:56:37 PM6/18/10
to nod...@googlegroups.com
In npm I use this:
http://github.com/isaacs/npm/blob/master/lib/utils/rm-rf.js


If you've installed npm, you can get at it this way:

var rm = require("npm/utils/rm-rf")

but of course, you probably want to just bundle it in your own app, so
you don't depend on npm unnecessarily.

--i

r...@tinyclouds.org

unread,
Jun 18, 2010, 4:07:10 PM6/18/10
to nod...@googlegroups.com
On Fri, Jun 18, 2010 at 7:34 AM, Ryan Gahl <ryan...@gmail.com> wrote:
> I had a need to delete a directory that wasn't empty, and had to resort to
> child_process.exec("rm -r " + dirName). So that leads me to the main Q: why
> is there no fs.rm() and fs.rmRecursive() or the like?

Because there is no syscall for a recursive delete. After 0.2 i may
add some convenience functions like this.

Ryan Gahl

unread,
Jun 18, 2010, 4:09:36 PM6/18/10
to nod...@googlegroups.com
On Fri, Jun 18, 2010 at 3:07 PM, <r...@tinyclouds.org> wrote:
Because there is no syscall for a recursive delete. After 0.2 i may
add some convenience functions like this.

Gotcha. Thanks. I'll either pull in Isaacs or Fabian's helpers or just stick with child_process.exec for now.

Fabian Jakobs

unread,
Jun 21, 2010, 10:50:33 AM6/21/10
to nod...@googlegroups.com

What about adding a flag to rmdir, which toggles whether delete is recursive.

rmdir = function(path, recursive, callback)

I think this makes sense but it will break the API and the one to one
mapping to the posix system calls. The other solution would be to add
a fs.rm function, which acts similar to the unix "rm" command. Would
you accept a patch for any of those?

Best Fabian

Reply all
Reply to author
Forward
0 new messages