1

Suppose I created a folder in "Documents" folder in my application sandbox and I called it "ID123".

From NSFileManager Class Reference I found out that I can create a new folder with one of these methods:

– createDirectoryAtURL:withIntermediateDirectories:attributes:error:
– createDirectoryAtPath:withIntermediateDirectories:attributes:error:

The question is how can I delete created directory?

2 Answers 2

2

You can use removeItemAtPath:error: OR removeItemAtURL:error: for doing this.

Like:

[[NSFileManager defaultManager] removeItemAtPath:yourPath error:nil];

or you can use:

[[NSFileManager defaultManager] removeItemAtURL:yourPathURL error:nil];
1
  • Using URL based methods are the preferred approach.
    – Abizern
    Nov 18, 2013 at 9:17
2

Depending on whether you're using the URL or path based approach, either use the...

- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error

or

- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error

...NSFileManager method.

For example:

NSError *removalError = nil;
if(![[NSFileManager defaultManager] removeItemAtPath:pathToFile error:&removalError]) {
    // Something went wrong.
    NSLog(@"%@", [removalError localizedDescription]);
}

In terms of recursive removal the supplied path (or URL depending on the method) can point at a directory containing items/sub-folders, etc. As per the Apple docs:

A path string indicating the file or directory to remove. If the path specifies a directory, the contents of that directory are recursively removed.

Incidentally, this is clearly defined within the "Creating and Deleting Items" section of the NSFileManager Class reference, so it would probably be worth the time to give that document a quick overview.

1
  • 2
    Ah... the joy of the spineless downvote without a reason. :-) Nov 18, 2013 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.