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.
source share