Realm.deleteFiles Implementation (Swift)

Anyone know the correct implementation for deleting the local Realm files when using MongoDB Realm Sync in Swift? We want to use this to force a refresh.

We can certainly do it at the finder level but since Realm offers that function, we would like to leverage it.

The SwiftReference for .deleteFiles shows it’s available but not working for us. We’ve ensured Realm not be currently open on any thread or in another process.

do {
    let config = Realm.Configuration()
    let result = try Realm.deleteFiles(for: config)

    if result == true {
        print("all files have been deleted")
    } else {
        print("deleting of files failed")
    }
} catch let error as NSError {
    print(error.localizedDescription)
}

Just prints it failed.

Hi Jay. What error you have in the catch block?
Could you check the access rights for the files? Are files actually existing?
Any info about error’s domain, code and userInfo?

Thanks for the response @Pavel_Yakimenko

It doesn’t throw an error. The result var is set to false and prints

deleting of files failed

The is a macOS app, no sandboxing and full access rights to files.

My guess is that since the file names and paths are different for Sync vs local, it’s not finding them.

So are the files existing? For sure, but again, it only fails for sync’d realms and works correctly for local only realms.

I can successfully delete the synced realm on mac (custom and default path) so there could be something else.
What call you are using to open realm?
Also why you’re unhappy with the existing realm sync mechanism? Is there something not working for you?

@Pavel_Yakimenko

I am not opening realm at all as if realm is open, it cannot be deleted. The Realm.delete is a Class function call so is there some kind of initialization that needs to be done?

Who said I was unhappy with Sync? We’ve been building on Realm for almost 5 years now and two years into development on our main project. We are heads deep into the project so being unhappy isn’t an option! lol.

Here’s the entire project

import RealmSwift

class ViewController: NSViewController {
    @IBAction func deleteLocalFilesAction(_ sender: Any) {
       self.deleteLocalRealm()
    }

   override func viewDidLoad() {
       super.viewDidLoad()
   }

   func deleteLocalRealm() {
       do {
           let config = Realm.Configuration()
           let result = try Realm.deleteFiles(for: config)

           if result == true {
               print("all files have been deleted")
           } else {
               print("deleting of files failed")
           }
       } catch let error as NSError {
           print(error.localizedDescription)
       }
   }

There’s no other code in the AppDelegate other than the default and the UI has one button action that calls deleteLocalRealm.

Realm.deleteFiles(for: Realm.Configuration()) will delete the default local Realm, and then it returns false because that file already doesn’t exist. To delete the local files for a specific sync Realm you need to pass in the config for that Realm instead.

@Thomas_Goyne

Yes, that’s what that function does as shown and as mentioned, it works correctly for non-sync’d realms because the default.realm file exists. Our expectation was that it would remove the local realm files the same way for either a local only Realm or a Sync’d realm.

So that actually lead to the solution, which is now clear but wasn’t before.

The file structure between non-sync and sync realms is different with the sync’d realm files having separate files for each partition

To delete the local files for a specific sync Realm you need to pass in the config for that Realm instead.

And there we go; Realm.delete will only delete the files for the partition specified within the passed config, not all files, to force a re-sync which was the original objective.

With updated code, it’s working correctly, keeping in mind that if you want a full re-sync, you need to call Realm.delete for each partition, passing in a config.

 do {
    let app = App(id: Constants.REALM_APP_ID)
    let user = app.currentUser
    let config = user?.configuration(partitionValue: Constants.REALM_PARTITION_VALUE)
    let result = try Realm.deleteFiles(for: config!)

    if result == true {
        print("all files have been deleted")
    } else {
        print("deleting of files failed")
    }
} catch let error as NSError {
    print(error.localizedDescription)
}

Thanks all!

1 Like

3 posts were split to a new topic: .realm cannot be deleted because it is currently opened