Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting all the files in the iPhone sandbox (documents folder)?

Tags:

ios

iphone

ios4

Is there an easy way to delete all the files(images) I saved in the documents folder of the app?

like image 859
Jinah Adam Avatar asked Jan 25 '11 12:01

Jinah Adam


People also ask

Can I delete the files folder on iPhone?

Go to the "Files" app and click "On My iPhone/iPad". Step 2. Find the file or folder you want to delete. If the files or folders are saved in iCloud but not in the internal storage, you need to remove these unwanted items from the iCloud Drive.

How delete multiple files IOS?

Press the Shift key to select multiple files or folders. In the confirmation pop-up window, select Delete to delete your files permanently from the cloud.


2 Answers

NSFileManager *fileMgr = [[[NSFileManager alloc] init] autorelease]; NSError *error = nil; NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]; if (error == nil) {     for (NSString *path in directoryContents) {         NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];         BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];         if (!removeSuccess) {             // Error handling             ...         }     } } else {     // Error handling     ... } 
like image 64
Ole Begemann Avatar answered Sep 29 '22 17:09

Ole Begemann


Code did not work with IOS 7 and Xcode 5 so edited to work with my app. Big credits to @Ole Begemann and @pablasso.

-(void)EmptySandbox {     NSFileManager *fileMgr = [[NSFileManager alloc] init];     NSError *error = nil;     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString *documentsDirectory = [paths objectAtIndex:0];     NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];      while (files.count > 0) {         NSString *documentsDirectory = [paths objectAtIndex:0];         NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];         if (error == nil) {             for (NSString *path in directoryContents) {                 NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];                 BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];                 files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];                 if (!removeSuccess) {                     // Error                 }             }         } else {             // Error         }     } } 
like image 42
Msmit1993 Avatar answered Sep 29 '22 16:09

Msmit1993