How to completely remove the Realm database from iOS?

Now I get the error Property types for 'value' property do not match. Old type 'float', new type 'double' Property types for 'value' property do not match. Old type 'float', new type 'double' How to clear the database or complete the migration successfully?

+7
source share
7 answers

To completely remove a Realm file from disk and start from scratch, simply use NSFileManager to delete it manually.

For example, to delete the default Realm file:

 NSFileManager.defaultManager().removeItemAtURL(Realm.Configuration.defaultConfiguration.fileURL!) 

If you want to save the Realm file, but completely clear it of objects, you can call deleteAll() to do this:

 let realm = try! Realm() try! realm.write { realm.deleteAll() } 

Update: I feel like I didn't mention this in my original answer. If you decide to delete the Realm file from disk, you must do this before opening it in any threads of your application. After opening, Realm internally caches a link to it, which will not be released, even if the file is deleted.

If you absolutely need to open a Realm file to check its contents before deleting, you can enclose it in autoreleasepool for this.

+31
source

While the other comments are correct, you should really look: https://realm.io/docs/swift/latest/#migrations

This gives a very clear explanation of how to migrate, and it really removes everything very simply and much better if it can help.

+1
source

Here's how to do it in Swift 4.1:

 FileManager.default.removeItem(at:Realm.Configuration.defaultConfiguration.fileURL!) 
+1
source

In addition to using NSFileManager to delete the file, as well as delete the .lock file and .management folder as well. Otherwise, if you try to recreate a region file with the same name, it will give an error saying that it cannot find it

+1
source

You tried

  let realm = try! Realm() realm.deleteAllObjects() 

You can also try to delete an area from the device by connecting it to the computer, going to Xcode and then to devices, and then find the current area and delete it.

0
source

Swift 4.2 To delete a database:

 func remove(realmURL: URL) { let realmURLs = [ realmURL, realmURL.appendingPathExtension("lock"), realmURL.appendingPathExtension("note"), realmURL.appendingPathExtension("management"), ] for URL in realmURLs { try? FileManager.default.removeItem(at: URL) } let url = Realm.Configuration.defaultConfiguration.fileURL! remove(realmURL: url) 

To clean the database:

 try? realm.write { realm.deleteAll() } 
0
source

Swift 4.2:

 func remove(realmURL: URL) { let realmURLs = [ realmURL, realmURL.appendingPathExtension("lock"), realmURL.appendingPathExtension("note"), realmURL.appendingPathExtension("management"), ] for URL in realmURLs { try? FileManager.default.removeItem(at: URL) } let url = Realm.Configuration.defaultConfiguration.fileURL! remove(realmURL: url) 
0
source

All Articles