How to delete all the files and directories in Documents directory?

I use Documents directory to save photos as jpeg file being used in my app.

I would like to delete all the files saved in Documents directory at one time.

Is there any way to do this?

Do I need to scan saved filename and delete them one by one?


Please advise me.

Answered by junkpile in 124711022

And just to expand on what Gargoyle said, I believe if you want to delete all the files in a directory but not delete the directory itself, then yes you will have to iterate over the files / subdirectories and delete them one by one. NSFileManager has a contentsOfDirectoryAtURL method for that purpose.

Use the NSFileManager method removeItemAtURL:error:


If you specify a directory, the contents of that directory are recursively removed.

Accepted Answer

And just to expand on what Gargoyle said, I believe if you want to delete all the files in a directory but not delete the directory itself, then yes you will have to iterate over the files / subdirectories and delete them one by one. NSFileManager has a contentsOfDirectoryAtURL method for that purpose.

Yes, you are right. I would like to delete all the files and directories in Documents directory, but Documents directory should remain (I guess I can not delete Documents directory, though).


I will use NSFileManager and delete them one by one.

Thank you for your advice.

As junkpile mentioned, I will use NSFileManager and delete them one by one.

Thanks for your advice.

How to delete all the files and directories in Documents directory?
 
 
Q