Search code examples
iosobjective-cnsdocumentdirectory

How to delete the file that is created in the Document directory


I created a database file using the following code

// Get the documents directory
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = dirPaths[0];

// Build the path to the database file


databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"MyDB.db"]];

How can I remove the file "MyDB.db" ?

Thanks in advance.


Solution

  • You can remove a file with the following

    NSError *error;
    if(![[NSFileManager defaultManager] removeItemAtPath:databasePath error:&error]) {
        NSLog(@"error while removing file: %@", error.localizedDescription);
    }