Search code examples
objective-cnsdocumentdirectory

removeItemAtPath behaving unexpectedly


I'm trying to remove a folder from the documents directory of my app with this code:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"downloads"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if ([eraseDevice isEqualToString:@"true"])
{
    if ([fileManager removeItemAtPath:downloadFolder error:NULL] == YES)
        NSLog(@"downloads folder deleted");
    else
        NSLog(@"erase downloads failed");
}

When invoked, the console prints downloads folder deleted but the directory remains.

Can someone tell me what's wrong with the code above or how to remove the folder in question? Thanks.

-EDIT to add NSLogs of documentsPath & downloadFolder-

/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/6C785921-89B8-49C9-8BFC-06564B95C72C/Documents
/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/6C785921-89B8-49C9-8BFC-06564B95C72C/Documents/downloads

Solution

  • 1) Is the directory empty? Not sure if this makes a difference but just wanted to check. 2) When you breakpoint over the removeItemAtPath:error: line, do you see the folder delete, or do you only check it later?