deleting all files in documents directory

I want to add a functionality in my app that if user press back button without saving his/her data, all saved files in documents directory get deleted… Now I dont know how to completely flush this directory. Can anybody help me here? [import]uid: 173824 topic_id: 35189 reply_id: 335189[/import]

it is working …
Although its not an issue but just for the curiosity that this loop not only delete the files in documents directory but also delete the complete folder /documents
Is it possible that the directory become empty but not deleted
On relaunching the project, it is there,so not an issue
Thank you [import]uid: 173824 topic_id: 35189 reply_id: 139912[/import]

Have a look at these links
http://docs.coronalabs.com/api/library/lfs/index.html
http://docs.coronalabs.com/api/library/os/index.html

 

local lfs = require "lfs"; local doc\_dir = system.DocumentsDirectory; local doc\_path = system.pathForFile("", doc\_dir); local resultOK, errorMsg; for file in lfs.dir(doc\_path) do local theFile = system.pathForFile(file, doc\_dir); 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

Ooops! That’s what I get for writing code in a post without testing… really sorry for that.
I forgot that it returned the system files “.” and “…” Ouch!

Anyway the code above has been modified to test for directories now and should only remove all files… [import]uid: 70847 topic_id: 35189 reply_id: 139916[/import]

well this too deleting \documents directory.
I dont know why as check is there but still…!! [import]uid: 173824 topic_id: 35189 reply_id: 140057[/import]

it is working …
Although its not an issue but just for the curiosity that this loop not only delete the files in documents directory but also delete the complete folder /documents
Is it possible that the directory become empty but not deleted
On relaunching the project, it is there,so not an issue
Thank you [import]uid: 173824 topic_id: 35189 reply_id: 139912[/import]

Have a look at these links
http://docs.coronalabs.com/api/library/lfs/index.html
http://docs.coronalabs.com/api/library/os/index.html

 

local lfs = require "lfs"; local doc\_dir = system.DocumentsDirectory; local doc\_path = system.pathForFile("", doc\_dir); local resultOK, errorMsg; for file in lfs.dir(doc\_path) do local theFile = system.pathForFile(file, doc\_dir); 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

Ooops! That’s what I get for writing code in a post without testing… really sorry for that.
I forgot that it returned the system files “.” and “…” Ouch!

Anyway the code above has been modified to test for directories now and should only remove all files… [import]uid: 70847 topic_id: 35189 reply_id: 139916[/import]

well this too deleting \documents directory.
I dont know why as check is there but still…!! [import]uid: 173824 topic_id: 35189 reply_id: 140057[/import]

Hey guys,

Can you help me figure out how to delete a folder found in the DocumentsDirectory?

So I have “Documents/gameContent” for example.

I just want to delete the “gameContent” folder only, because I have other stuff in the “Documents” folder that needs to stay untouched.

You can use lfs.rmdir() or os.remove() to remove directories.

So calling os.remove() on a path to my “gameContent” will delete it and everything contained inside it?  Here is what I am currently trying.

--Used to delete volume content if requested by the user menuFunctions.deleteVolumeContent = function( event ) if global.volumeToDelete ~= 0 then local lfs = require "lfs"; local doc\_dir = system.DocumentsDirectory local doc\_path = system.pathForFile("", doc\_dir); --Change directory to the Documents directory lfs.chdir( doc\_path ) --Change directory to the volume 2 directory lfs.chdir( lfs.currentdir() .. "/volume2") --Get current path and hold in variable local currentPath = lfs.currentdir() local resultOK, errorMsg; for file in lfs.dir(currentPath) do local theFile = system.pathForFile(file, doc\_dir); 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 else native.showAlert("Alert", "You must select a volume to delete first.", {"Okay"}) end end

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:

Hey guys,

Can you help me figure out how to delete a folder found in the DocumentsDirectory?

So I have “Documents/gameContent” for example.

I just want to delete the “gameContent” folder only, because I have other stuff in the “Documents” folder that needs to stay untouched.

You can use lfs.rmdir() or os.remove() to remove directories.

So calling os.remove() on a path to my “gameContent” will delete it and everything contained inside it?  Here is what I am currently trying.

--Used to delete volume content if requested by the user menuFunctions.deleteVolumeContent = function( event ) if global.volumeToDelete ~= 0 then local lfs = require "lfs"; local doc\_dir = system.DocumentsDirectory local doc\_path = system.pathForFile("", doc\_dir); --Change directory to the Documents directory lfs.chdir( doc\_path ) --Change directory to the volume 2 directory lfs.chdir( lfs.currentdir() .. "/volume2") --Get current path and hold in variable local currentPath = lfs.currentdir() local resultOK, errorMsg; for file in lfs.dir(currentPath) do local theFile = system.pathForFile(file, doc\_dir); 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 else native.showAlert("Alert", "You must select a volume to delete first.", {"Okay"}) end end