How can I delete a folder and its subfolders in Objective-C / C on iOS?
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
In the search box (top right), type NOT kind:folder . You will now see a list of all files in your current folder and all its subfolders. Use Control-A to select all the files. Now you can move them all to another folder.
Run the command RMDIR /Q/S foldername to delete the folder and all of its subfolders.
You can use NSFileManager
:
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:pathToFolder error:nil];
This question is similar to this. You can check the link or follow the answer I've provided: You can get document directory by using this:
NSString *directoryPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/"];
** Remove full directory path by using this:
BOOL success = [fileManager removeItemAtPath:directoryPath error:nil]; if (!success) { NSLog(@"Directory delete failed"); }
** Remove the contents of that directory using this:
NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:directoryPath]) { NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:directoryPath]; NSString *documentsName; while (documentsName = [dirEnum nextObject]) { NSString *filePath = [directoryPath stringByAppendingString:documentsName]; BOOL isFileDeleted = [fileManager removeItemAtPath:filePath error:nil]; if(isFileDeleted == NO) { NSLog(@"All Contents not removed"); break; } } NSLog(@"All Contents Removed"); }
** You can edit directoryPath
as per your requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With