How to Completely Remove Realm Database from iOS

How to completely remove Realm database from iOS?

To completely delete the Realm file from disk and start from scratch, it's simply a matter of using NSFileManager to manually delete it.

For example, to delete the default Realm file:

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

If you want to preserve the Realm file, but completely empty it of objects, you can call deleteAll() to do so:

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

Update: I feel I neglected to mention this in my original answer. If you choose to delete the Realm file from disk, you must do so before you've opened it on any threads in your app. Once it's opened, Realm will internally cache a reference to it, which won't be released even if the file is deleted.

If you absolutely do need to open the Realm file to check its contents before deletion, you can enclose it in an autoreleasepool to do this.

Clear complete Realm Database

Update:

Since posting, a new method has been added to delete all objects (as jpsim has already mentioned):

// Obj-C
[realm beginWriteTransaction];
[realm deleteAllObjects];
[realm commitWriteTransaction];


// Swift
try! realm.write {
realm.deleteAll()
}

Note that these methods will not alter the data structure; they only delete the existing records. If you are looking to alter the realm model properties without writing a migration (i.e., as you might do in development) the old solution below may still be useful.

Original Answer:

You could simply delete the realm file itself, as they do in their sample code for storing a REST response:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...

// Ensure we start with an empty database
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:nil];

//...
}

Update regarding your comment:

If you need to be sure that the realm database is no longer in use, you could put realm's notifications to use. If you were to increment an openWrites counter before each write, then you could run a block when each write completes:

self.notificationToken = [realm addNotificationBlock:^(NSString *notification, RLMRealm * realm) {
if([notification isEqualToString:RLMRealmDidChangeNotification]) {
self.openWrites = self.openWrites - 1;

if(!self.openWrites && self.isUserLoggedOut) {
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:nil];
}
}
}];

deleting object from realm database.

I found out that that problem comes from "delete" word. It seems that this word preserved for Xcode. When I call it , it goes to another delete method in another place not mine. I changed the name to for example deleteSomethings and it worked.

How can I delete local realm in macOS?

I have a button with the following code to remove Realm data in Swift 4

func doDelete() {
let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
let realmURLs = [
realmURL,
realmURL.appendingPathExtension("lock"),
realmURL.appendingPathExtension("note"),
realmURL.appendingPathExtension("management")
]
for URL in realmURLs {
do {
try FileManager.default.removeItem(at: URL)
} catch let error as NSError {
print(error.localizedDescription)
}
}
}

alternately you can manually remove the database portion using the finder by deleting the folder:

~/Library/Application Support/com.company_name.app_name

the bundle identifier (found in the General settings for your app in Xcode) is the last part of the path. The path may vary depending on sandboxing, iOS/macOS and the bundle name.

Edit

With sandboxing on the location is

~/Library/Containers/com.company_name.app_name


Related Topics



Leave a reply



Submit