diff --git a/src/lua/fio.lua b/src/lua/fio.lua
index 3fda6e5322929e9bfb6914b322cac185e31069de..d5531c1542c6ca1e69cc5dc5ac5b0838b7cab3ea 100644
--- a/src/lua/fio.lua
+++ b/src/lua/fio.lua
@@ -382,8 +382,12 @@ fio.rmtree = function(path)
     for i, f in ipairs(ls) do
         local tmppath = fio.pathjoin(path, f)
         local st = fio.stat(tmppath)
-        if st and st:is_dir() then
-            st, err = fio.rmtree(tmppath)
+        if st then
+            if st:is_dir() then
+                st, err = fio.rmtree(tmppath)
+            else
+                st, err = fio.unlink(tmppath)
+            end
             if err ~= nil  then
                 return nil, err
             end
diff --git a/test/app/fio.result b/test/app/fio.result
index 6c4609855f0dc2350a1d8ecdb7e6fb22b401c464..3f8d77e25f0471818e44f235a00a1e2ce22d9a54 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -413,6 +413,27 @@ fio.rmdir(dir2)
   - false
   - 'fio: No such file or directory'
 ...
+-- gh-3258 rmtree should remove directories with files
+fio.mktree('tmp2/tmp3/tmp4')
+---
+- true
+...
+fh = fio.open('tmp2/tmp3/tmp4/tmp.txt', {'O_RDWR', 'O_CREAT'})
+---
+...
+fh:close()
+---
+- true
+...
+fio.rmtree('tmp2')
+---
+- true
+...
+fio.stat('tmp2')
+---
+- null
+- 'fio: No such file or directory'
+...
 fio.rmdir(tmpdir)
 ---
 - true
diff --git a/test/app/fio.test.lua b/test/app/fio.test.lua
index 0850413d9b96cccf05d7d73e743ef9e1e836658a..c2c8e689b4a331236bfd4b97426e04b6583136cc 100644
--- a/test/app/fio.test.lua
+++ b/test/app/fio.test.lua
@@ -131,6 +131,14 @@ fio.rmdir(dir2)
 
 { fio.unlink(file1), fio.unlink(file2), fio.unlink(file3), fio.unlink(file4) }
 { fio.unlink(file1), fio.unlink(file2), fio.unlink(file3), fio.unlink(file4) }
+
+-- gh-3258 rmtree should remove directories with files
+fio.mktree('tmp2/tmp3/tmp4')
+fh = fio.open('tmp2/tmp3/tmp4/tmp.txt', {'O_RDWR', 'O_CREAT'})
+fh:close()
+fio.rmtree('tmp2')
+fio.stat('tmp2')
+
 fio.rmdir(tmpdir)
 fio.rmdir(tmpdir)