3

I'm trying to remove non-empty directory in Lua but without success,

I tried the following:

os.remove(path_to_dir)

And got the error: Directory not empty 39 When 39 is the number of files in path_to_dir

Also tried:

require ('lfs')
lfs.rmdir(path_to_dir)

And got the error: Directory not empty'

Worth to mention that I did chmod -R a+rX * to path_to_dir

Thanks for the help.

IceCube
  • 405
  • 1
  • 5
  • 12

4 Answers4

3

You can either follow @EgorSkriptunoff's suggestion and use OS-specific commands to remove non-empty directories or get the list of files/sub-directories using lfs (for example, as described in this SO answer) and delete them one-by-one using os.remove.

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • It's hard to believe that Lua doesn't provide a function that remove non-empty directory . – IceCube Jun 15 '16 at 17:33
  • 4
    The reason that Lua doesn't provide that is that C doesn't provide that either, in a standard, cross-platform way. If you want a fully-featured filesystem api you need to use one of the add-ons to lua, like luafilesystem or the others that were mentioned. – Chris Beck Jun 15 '16 at 18:06
  • It makes sense and is in fact really just consequent for Lua to not provide such a functionality. The thing is, he **cannot** just use an additional module like luafilesystem. I think that he (and know that I) would be willing to do so. But even using lfs, one *still* has to implement an own function for something as simple as deleting a directory. That is kind of sad. – Chris K Jun 09 '17 at 09:03
  • @ChrisK. The philosophy in the Lua community is that the entire environment should be cross-platform and embeddable. If you disagree with that, feel free to publish your own filesystem api to luarocks, but personally I'd rather write ten lines of code than try to justify embedding a multi-megabyte library onto my system. If I wanted to do that I would just switch to NodeJS and `npm install` half the internet onto my machine. – yyny Feb 23 '18 at 15:59
2

With path library you can do

function rmdir(p)
  path.each(path.join(p,"*"), function(P)
    path.remove(P)
  end,{
    param = "f";   -- request full path
    delay = true;   -- use snapshot of directory
    recurse = true; -- include subdirs
    reverse = true; -- subdirs at first 
  })
  path.remove(p)
end
moteus
  • 2,187
  • 1
  • 13
  • 16
2

With lua lfs, you can implement a recursive function to do this.

local lfs = require('lfs')

local deletedir
deletedir = function(dir)
    for file in lfs.dir(dir) do
        local file_path = dir..'/'..file
        if file ~= "." and file ~= ".." then
            if lfs.attributes(file_path, 'mode') == 'file' then
                os.remove(file_path)
                print('remove file',file_path)
            elseif lfs.attributes(file_path, 'mode') == 'directory' then
                print('dir', file_path)
                deletedir(file_path)
            end
        end
    end
    lfs.rmdir(dir)
    print('remove dir',dir)
end


deletedir('tmp')
Everett
  • 339
  • 2
  • 10
1

Depending on your os, you could just do this:

os.execute("rm --recursive " .. path_to_directory)

(this example is for linux)

DarkWiiPlayer
  • 6,871
  • 3
  • 23
  • 38