Deleting All Files from system.DocumentsDirectory in corona

1.9k Views Asked by At

I am new to corona and want to add a functionality in my app that on pressing back button, all data stored by the user get deleted from the documents directory. In short I want to know is there a way to empty documents directory?

2

There are 2 best solutions below

0
On

Yes, you would use the LFS (Lua File System) module for that. See:

http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/

0
On

Use this to delete all the files in /documents directory

 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