deleting all files in documents directory

You have a few errors in your function, and you don’t need to change directory for this to work.

Look at the following modified code:

menuFunctions.deleteVolumeContent = function( event ) if global.volumeToDelete ~= 0 then local lfs = require "lfs"; local doc\_dir = system.DocumentsDirectory; local doc\_path = system.pathForFile("volume2", doc\_dir); local resultOK, errorMsg; --remove files from directory for file in lfs.dir(doc\_path) do local theFile = doc\_path.."/"..file; if (lfs.attributes(theFile, "mode") ~= "directory") then resultOK, errorMsg = os.remove(theFile); if (resultOK) then print(file.." removed"); else print("Error removing file: "..file..":"..errorMsg); end end end -- -- remove directory -- resultOK, errorMsg = os.remove(doc\_path); -- -- if (resultOK) then -- print(doc\_path.." removed"); -- else -- print("Error removing file: "..doc\_path..":"..errorMsg); -- end else native.showAlert("Alert", "You must select a volume to delete first.", {"Okay"}) end end

This will delete the contents of your ‘volume2’ directory.

If you also want to delete ‘volume2’ after the files are removed, just un-comment the block at the end.

@ingemar,

Thank you for helping me out.  The code you supplied does delete the files found directly inside “volume2”, but I actually have several more layers of folders inside of “volume2” that are not getting traversed.  How could your code traverse all folders and delete everything?  Here is a image to show the layout of the “volume2” folder and its layers of folders.

  ScreenShot2013-07-23at95758AM_zps3b8e8fe

Might need to do some kind of recursion?

Yep, recursion is the key… something like this should do it.

(Be careful though, it will delete everything without asking)

local lfs = require "lfs"; --forward declaration local deleteDirectory; deleteDirectory = function(dir) local resultOK, errorMsg; --remove files from directory for file in lfs.dir(dir) do if ((file == ".") or (file == "..")) then -- skip system files -- do nothing else local theFile = dir.."/"..file; if (lfs.attributes(theFile, "mode") == "directory") then deleteDirectory(theFile); else resultOK, errorMsg = os.remove(theFile); if (not resultOK) then print("Error removing file: "..file..":"..errorMsg); end end end end -- remove directory resultOK, errorMsg = os.remove(dir); if (not resultOK) then print("Error removing directory: "..dir..":"..errorMsg); end end menuFunctions.deleteVolumeContent = function( event ) if global.volumeToDelete ~= 0 then local doc\_dir = system.DocumentsDirectory; local doc\_path = system.pathForFile("volume2", doc\_dir); deleteDirectory(doc\_path); else native.showAlert("Alert", "You must select a volume to delete first.", {"Okay"}) end end

@ingemar,

That did it!  Thank you so much for taking the time to help me out.  In a crunch to get my app uploaded in the next few days and this was one of the last things to figure out. Thank you again.

:slight_smile:

I’m having an os.remove issue. I’m using code to erase a variety of stored player info including a snapshot they can take of themselves which should be erased with the following code:
local results3, reason = os.remove(system.pathForFile(“newImage21.jpg”, system.DocumentsDirectory))
local results4, reason = os.remove(system.pathForFile(“newImage22.jpg”, system.DocumentsDirectory))
local results5, reason = os.remove(system.pathForFile(“newImage23.jpg”, system.DocumentsDirectory))
local results6, reason = os.remove(system.pathForFile(“newImage24.jpg”, system.DocumentsDirectory))
the problem is that it’s also erasing the other four sets of images: newImage11 through 14.jpg, newImage31 through 34.jpg, etc., up to 51-54. it doesn’t matter which set of images the button in each profile tells to go away, it basically wipes out the whole she-bang. any ideas? please… thanks!!!

I just changed local results3, 4, etc. to all results00’s in one profile, all 11’s, in another, etc., and there seems to no longer be an issue. whew!

I was wrong…that didn’t work.

I’m having an os.remove issue. I’m using code to erase a variety of stored player info including a snapshot they can take of themselves which should be erased with the following code:
local results3, reason = os.remove(system.pathForFile(“newImage21.jpg”, system.DocumentsDirectory))
local results4, reason = os.remove(system.pathForFile(“newImage22.jpg”, system.DocumentsDirectory))
local results5, reason = os.remove(system.pathForFile(“newImage23.jpg”, system.DocumentsDirectory))
local results6, reason = os.remove(system.pathForFile(“newImage24.jpg”, system.DocumentsDirectory))
the problem is that it’s also erasing the other four sets of images: newImage11 through 14.jpg, newImage31 through 34.jpg, etc., up to 51-54. it doesn’t matter which set of images the button in each profile tells to go away, it basically wipes out the whole she-bang. any ideas? please… thanks!!!

I just changed local results3, 4, etc. to all results00’s in one profile, all 11’s, in another, etc., and there seems to no longer be an issue. whew!

I was wrong…that didn’t work.