Clear complete Realm Database2019 Community Moderator ElectionHow to completely destroy and recreate a Realm databaseRealm objects intermittently not being present when app launchesiOS background fetch time limit crashHow to find my realm file?This action could not be completed. Try Again (-22421)Reusing snapshots of Realm databasesHow to use Realm “live” objects and keep database layer decoupled from other layers(business, UI, etc)?Realm objects intermittently not being present when app launchesRealm file size in iOS appRealm Swift inverse relationships many-to-manyRealm thread safe object with singletonRealm in installed app, but not Instant App?
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
Can anyone tell me why this program fails?
Replacing Windows 7 security updates with anti-virus?
When do we add an hyphen (-) to a complex adjective word?
I need to drive a 7/16" nut but am unsure how to use the socket I bought for my screwdriver
What is the greatest age difference between a married couple in Tanach?
Be in awe of my brilliance!
Latest web browser compatible with Windows 98
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Making a sword in the stone, in a medieval world without magic
What is IP squat space
Do I need life insurance if I can cover my own funeral costs?
Calculus II Professor will not accept my correct integral evaluation that uses a different method, should I bring this up further?
Is a lawful good "antagonist" effective?
PlotLabels with equations not expressions
Sword in the Stone story where the sword was held in place by electromagnets
How do anti-virus programs start at Windows boot?
Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?
Why must traveling waves have the same amplitude to form a standing wave?
Know when to turn notes upside-down(eighth notes, sixteen notes, etc.)
What options are left, if Britain cannot decide?
Happy pi day, everyone!
How to simplify this time periods definition interface?
How to deal with a cynical class?
Clear complete Realm Database
2019 Community Moderator ElectionHow to completely destroy and recreate a Realm databaseRealm objects intermittently not being present when app launchesiOS background fetch time limit crashHow to find my realm file?This action could not be completed. Try Again (-22421)Reusing snapshots of Realm databasesHow to use Realm “live” objects and keep database layer decoupled from other layers(business, UI, etc)?Realm objects intermittently not being present when app launchesRealm file size in iOS appRealm Swift inverse relationships many-to-manyRealm thread safe object with singletonRealm in installed app, but not Instant App?
I'm playing around with realm (currently 0.85.0) and my application uses the database to store user-specific data such as the contacts of the current user. When the user decides to log out I need to remove every single bit of information about the user and the most obvious, simple and clean thing in my opinion would be to wipe the complete realm. Unfortunately, the Cocoa lib doesn't provide that functionality.
Currently, I'm stuck with the following
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteObjects:[MyRealmClass1 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass2 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass3 allObjectsInRealm:realm]];
[realm commitWriteTransaction];
any better ideas?
thanks
ios realm
add a comment |
I'm playing around with realm (currently 0.85.0) and my application uses the database to store user-specific data such as the contacts of the current user. When the user decides to log out I need to remove every single bit of information about the user and the most obvious, simple and clean thing in my opinion would be to wipe the complete realm. Unfortunately, the Cocoa lib doesn't provide that functionality.
Currently, I'm stuck with the following
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteObjects:[MyRealmClass1 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass2 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass3 allObjectsInRealm:realm]];
[realm commitWriteTransaction];
any better ideas?
thanks
ios realm
add a comment |
I'm playing around with realm (currently 0.85.0) and my application uses the database to store user-specific data such as the contacts of the current user. When the user decides to log out I need to remove every single bit of information about the user and the most obvious, simple and clean thing in my opinion would be to wipe the complete realm. Unfortunately, the Cocoa lib doesn't provide that functionality.
Currently, I'm stuck with the following
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteObjects:[MyRealmClass1 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass2 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass3 allObjectsInRealm:realm]];
[realm commitWriteTransaction];
any better ideas?
thanks
ios realm
I'm playing around with realm (currently 0.85.0) and my application uses the database to store user-specific data such as the contacts of the current user. When the user decides to log out I need to remove every single bit of information about the user and the most obvious, simple and clean thing in my opinion would be to wipe the complete realm. Unfortunately, the Cocoa lib doesn't provide that functionality.
Currently, I'm stuck with the following
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteObjects:[MyRealmClass1 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass2 allObjectsInRealm:realm]];
[realm deleteObjects:[MyRealmClass3 allObjectsInRealm:realm]];
[realm commitWriteTransaction];
any better ideas?
thanks
ios realm
ios realm
edited Apr 23 '15 at 20:25
Michael Alan Huff
3,21232142
3,21232142
asked Sep 26 '14 at 19:44
floriankruegerfloriankrueger
1,63021322
1,63021322
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
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];
];
Yes, I already tried that but the problem is that this approach gets really nasty when there are some threads in the background who try to do something on the realm while the user decides to log out. Deleting the file on disk has the disadvantage that it ignores the transactions which might currently be open and eventually causes the app to crash. And I really don't want to start synchronizing all my database accesses .. Thanks anyways :)
– floriankrueger
Sep 26 '14 at 20:35
2
Hey Tim from Realm here. DonamiteIsTnt has the right idea but you’re right that there are edge cases to this approach. We will be introducing a method to delete realm files in a safe way very soon. Sorry about this!
– timanglade
Sep 28 '14 at 3:24
@timanglade Any word on how this method is coming along? I can't seem to find it, but it would be a great feature to be able to just delete and recreate a realm file
– cjwirth
Feb 10 '15 at 1:59
@cjwirth as described below, it’s here: realm.io/docs/cocoa/api/Classes/RLMRealm.html#//api/name/…
– timanglade
Feb 11 '15 at 7:15
Ah, thanks... I was kind of hoping you meant an api to delete the file. We only use it as a cache, and deleting/recreating the file is easier than dealing with migrations.
– cjwirth
Feb 11 '15 at 10:46
|
show 6 more comments
As of realm 0.87.0, it's now possible to delete all realm contents by calling [[RLMRealm defaultRealm] deleteAllObjects]
from a write transaction.
From Swift, it looks like this: RLMRealm.defaultRealm().deleteAllObjects()
3
This one won't work if there's pending migration. Use @DonamiteIsTnt answer if you want to bypass migration
– Le Duc Duy
Jan 24 '15 at 9:09
add a comment |
RealmSwift: Simple reset using a flag
Tried the above answers, but posting one more simple way to delete the realm file instead of migrating:
Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true
This simply sets a flag so that Realm can delete the existing file rather than crash on try! Realm()
Instead of manually deleting the file
Thought that was simpler than the Swift version of the answer above:
guard let path = Realm.Configuration.defaultConfiguration.fileURL?.absoluteString else
fatalError("no realm path")
do
try NSFileManager().removeItemAtPath(path)
catch
fatalError("couldn't remove at path")
I don't know in which version exactly that was introduced but usingdeleteRealmIfMigrationNeeded
is actually the correct way afaik now.
– floriankrueger
Nov 4 '16 at 11:23
1
To the top! This is the correct answer!
– Alex Bartiş
Mar 12 '17 at 8:57
add a comment |
In case someone stumbles on this question looking for a way to do this in Swift, this is just
DonamiteIsTnt's answer rewritten. I've added this function to a custom utility class so I can do MyAppUtilities.purgeRealm()
during testing & debugging
func purgeRealm()
NSFileManager.defaultManager().removeItemAtPath(RLMRealm.defaultRealmPath(), error: nil)
Note: If you find yourself in need of clearing data you might just check out Realm's new realm.addOrUpdateObject()
feature which allows you to replace existing data by its primary key with the new data. This way you're not continually adding new data. Just replacing "old" data. If you do use addOrUpdateObject()
make sure you override your model's primaryKey
class function so Realm knows which property is your primary key. In Swift, for example:
override class func primaryKey() -> String
return "_id"
add a comment |
I ran into this fun little issue. So instead i queried the schema version directly before running the schemamigration.
NSError *error = NULL;
NSUInteger currentSchemaVersion = [RLMRealm schemaVersionAtPath:[RLMRealm defaultRealmPath] error:&error];
if (currentSchemaVersion == RLMNotVersioned)
// new db, skip
else if (currentSchemaVersion < 26)
// kill local db
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:&error];
if (error)
MRLogError(error);
else if (error)
// for good measure...
MRLogError(error);
// perform realm migration
[RLMRealm setSchemaVersion:26
forRealmAtPath:[RLMRealm defaultRealmPath]
withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion)
];
nice idea, I'm definately going to try that :) thank you!
– floriankrueger
May 28 '15 at 5:24
add a comment |
You can also go to the location where your realm file is stored, delete that file from there and next time when you open realm after running app, you will see the empty realm database in browser.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26067193%2fclear-complete-realm-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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];
];
Yes, I already tried that but the problem is that this approach gets really nasty when there are some threads in the background who try to do something on the realm while the user decides to log out. Deleting the file on disk has the disadvantage that it ignores the transactions which might currently be open and eventually causes the app to crash. And I really don't want to start synchronizing all my database accesses .. Thanks anyways :)
– floriankrueger
Sep 26 '14 at 20:35
2
Hey Tim from Realm here. DonamiteIsTnt has the right idea but you’re right that there are edge cases to this approach. We will be introducing a method to delete realm files in a safe way very soon. Sorry about this!
– timanglade
Sep 28 '14 at 3:24
@timanglade Any word on how this method is coming along? I can't seem to find it, but it would be a great feature to be able to just delete and recreate a realm file
– cjwirth
Feb 10 '15 at 1:59
@cjwirth as described below, it’s here: realm.io/docs/cocoa/api/Classes/RLMRealm.html#//api/name/…
– timanglade
Feb 11 '15 at 7:15
Ah, thanks... I was kind of hoping you meant an api to delete the file. We only use it as a cache, and deleting/recreating the file is easier than dealing with migrations.
– cjwirth
Feb 11 '15 at 10:46
|
show 6 more comments
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];
];
Yes, I already tried that but the problem is that this approach gets really nasty when there are some threads in the background who try to do something on the realm while the user decides to log out. Deleting the file on disk has the disadvantage that it ignores the transactions which might currently be open and eventually causes the app to crash. And I really don't want to start synchronizing all my database accesses .. Thanks anyways :)
– floriankrueger
Sep 26 '14 at 20:35
2
Hey Tim from Realm here. DonamiteIsTnt has the right idea but you’re right that there are edge cases to this approach. We will be introducing a method to delete realm files in a safe way very soon. Sorry about this!
– timanglade
Sep 28 '14 at 3:24
@timanglade Any word on how this method is coming along? I can't seem to find it, but it would be a great feature to be able to just delete and recreate a realm file
– cjwirth
Feb 10 '15 at 1:59
@cjwirth as described below, it’s here: realm.io/docs/cocoa/api/Classes/RLMRealm.html#//api/name/…
– timanglade
Feb 11 '15 at 7:15
Ah, thanks... I was kind of hoping you meant an api to delete the file. We only use it as a cache, and deleting/recreating the file is easier than dealing with migrations.
– cjwirth
Feb 11 '15 at 10:46
|
show 6 more comments
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];
];
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];
];
edited Jan 25 '16 at 22:47
answered Sep 26 '14 at 19:55
DonVaughnDonVaughn
10.2k33948
10.2k33948
Yes, I already tried that but the problem is that this approach gets really nasty when there are some threads in the background who try to do something on the realm while the user decides to log out. Deleting the file on disk has the disadvantage that it ignores the transactions which might currently be open and eventually causes the app to crash. And I really don't want to start synchronizing all my database accesses .. Thanks anyways :)
– floriankrueger
Sep 26 '14 at 20:35
2
Hey Tim from Realm here. DonamiteIsTnt has the right idea but you’re right that there are edge cases to this approach. We will be introducing a method to delete realm files in a safe way very soon. Sorry about this!
– timanglade
Sep 28 '14 at 3:24
@timanglade Any word on how this method is coming along? I can't seem to find it, but it would be a great feature to be able to just delete and recreate a realm file
– cjwirth
Feb 10 '15 at 1:59
@cjwirth as described below, it’s here: realm.io/docs/cocoa/api/Classes/RLMRealm.html#//api/name/…
– timanglade
Feb 11 '15 at 7:15
Ah, thanks... I was kind of hoping you meant an api to delete the file. We only use it as a cache, and deleting/recreating the file is easier than dealing with migrations.
– cjwirth
Feb 11 '15 at 10:46
|
show 6 more comments
Yes, I already tried that but the problem is that this approach gets really nasty when there are some threads in the background who try to do something on the realm while the user decides to log out. Deleting the file on disk has the disadvantage that it ignores the transactions which might currently be open and eventually causes the app to crash. And I really don't want to start synchronizing all my database accesses .. Thanks anyways :)
– floriankrueger
Sep 26 '14 at 20:35
2
Hey Tim from Realm here. DonamiteIsTnt has the right idea but you’re right that there are edge cases to this approach. We will be introducing a method to delete realm files in a safe way very soon. Sorry about this!
– timanglade
Sep 28 '14 at 3:24
@timanglade Any word on how this method is coming along? I can't seem to find it, but it would be a great feature to be able to just delete and recreate a realm file
– cjwirth
Feb 10 '15 at 1:59
@cjwirth as described below, it’s here: realm.io/docs/cocoa/api/Classes/RLMRealm.html#//api/name/…
– timanglade
Feb 11 '15 at 7:15
Ah, thanks... I was kind of hoping you meant an api to delete the file. We only use it as a cache, and deleting/recreating the file is easier than dealing with migrations.
– cjwirth
Feb 11 '15 at 10:46
Yes, I already tried that but the problem is that this approach gets really nasty when there are some threads in the background who try to do something on the realm while the user decides to log out. Deleting the file on disk has the disadvantage that it ignores the transactions which might currently be open and eventually causes the app to crash. And I really don't want to start synchronizing all my database accesses .. Thanks anyways :)
– floriankrueger
Sep 26 '14 at 20:35
Yes, I already tried that but the problem is that this approach gets really nasty when there are some threads in the background who try to do something on the realm while the user decides to log out. Deleting the file on disk has the disadvantage that it ignores the transactions which might currently be open and eventually causes the app to crash. And I really don't want to start synchronizing all my database accesses .. Thanks anyways :)
– floriankrueger
Sep 26 '14 at 20:35
2
2
Hey Tim from Realm here. DonamiteIsTnt has the right idea but you’re right that there are edge cases to this approach. We will be introducing a method to delete realm files in a safe way very soon. Sorry about this!
– timanglade
Sep 28 '14 at 3:24
Hey Tim from Realm here. DonamiteIsTnt has the right idea but you’re right that there are edge cases to this approach. We will be introducing a method to delete realm files in a safe way very soon. Sorry about this!
– timanglade
Sep 28 '14 at 3:24
@timanglade Any word on how this method is coming along? I can't seem to find it, but it would be a great feature to be able to just delete and recreate a realm file
– cjwirth
Feb 10 '15 at 1:59
@timanglade Any word on how this method is coming along? I can't seem to find it, but it would be a great feature to be able to just delete and recreate a realm file
– cjwirth
Feb 10 '15 at 1:59
@cjwirth as described below, it’s here: realm.io/docs/cocoa/api/Classes/RLMRealm.html#//api/name/…
– timanglade
Feb 11 '15 at 7:15
@cjwirth as described below, it’s here: realm.io/docs/cocoa/api/Classes/RLMRealm.html#//api/name/…
– timanglade
Feb 11 '15 at 7:15
Ah, thanks... I was kind of hoping you meant an api to delete the file. We only use it as a cache, and deleting/recreating the file is easier than dealing with migrations.
– cjwirth
Feb 11 '15 at 10:46
Ah, thanks... I was kind of hoping you meant an api to delete the file. We only use it as a cache, and deleting/recreating the file is easier than dealing with migrations.
– cjwirth
Feb 11 '15 at 10:46
|
show 6 more comments
As of realm 0.87.0, it's now possible to delete all realm contents by calling [[RLMRealm defaultRealm] deleteAllObjects]
from a write transaction.
From Swift, it looks like this: RLMRealm.defaultRealm().deleteAllObjects()
3
This one won't work if there's pending migration. Use @DonamiteIsTnt answer if you want to bypass migration
– Le Duc Duy
Jan 24 '15 at 9:09
add a comment |
As of realm 0.87.0, it's now possible to delete all realm contents by calling [[RLMRealm defaultRealm] deleteAllObjects]
from a write transaction.
From Swift, it looks like this: RLMRealm.defaultRealm().deleteAllObjects()
3
This one won't work if there's pending migration. Use @DonamiteIsTnt answer if you want to bypass migration
– Le Duc Duy
Jan 24 '15 at 9:09
add a comment |
As of realm 0.87.0, it's now possible to delete all realm contents by calling [[RLMRealm defaultRealm] deleteAllObjects]
from a write transaction.
From Swift, it looks like this: RLMRealm.defaultRealm().deleteAllObjects()
As of realm 0.87.0, it's now possible to delete all realm contents by calling [[RLMRealm defaultRealm] deleteAllObjects]
from a write transaction.
From Swift, it looks like this: RLMRealm.defaultRealm().deleteAllObjects()
answered Nov 5 '14 at 17:07
jpsimjpsim
12.2k34062
12.2k34062
3
This one won't work if there's pending migration. Use @DonamiteIsTnt answer if you want to bypass migration
– Le Duc Duy
Jan 24 '15 at 9:09
add a comment |
3
This one won't work if there's pending migration. Use @DonamiteIsTnt answer if you want to bypass migration
– Le Duc Duy
Jan 24 '15 at 9:09
3
3
This one won't work if there's pending migration. Use @DonamiteIsTnt answer if you want to bypass migration
– Le Duc Duy
Jan 24 '15 at 9:09
This one won't work if there's pending migration. Use @DonamiteIsTnt answer if you want to bypass migration
– Le Duc Duy
Jan 24 '15 at 9:09
add a comment |
RealmSwift: Simple reset using a flag
Tried the above answers, but posting one more simple way to delete the realm file instead of migrating:
Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true
This simply sets a flag so that Realm can delete the existing file rather than crash on try! Realm()
Instead of manually deleting the file
Thought that was simpler than the Swift version of the answer above:
guard let path = Realm.Configuration.defaultConfiguration.fileURL?.absoluteString else
fatalError("no realm path")
do
try NSFileManager().removeItemAtPath(path)
catch
fatalError("couldn't remove at path")
I don't know in which version exactly that was introduced but usingdeleteRealmIfMigrationNeeded
is actually the correct way afaik now.
– floriankrueger
Nov 4 '16 at 11:23
1
To the top! This is the correct answer!
– Alex Bartiş
Mar 12 '17 at 8:57
add a comment |
RealmSwift: Simple reset using a flag
Tried the above answers, but posting one more simple way to delete the realm file instead of migrating:
Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true
This simply sets a flag so that Realm can delete the existing file rather than crash on try! Realm()
Instead of manually deleting the file
Thought that was simpler than the Swift version of the answer above:
guard let path = Realm.Configuration.defaultConfiguration.fileURL?.absoluteString else
fatalError("no realm path")
do
try NSFileManager().removeItemAtPath(path)
catch
fatalError("couldn't remove at path")
I don't know in which version exactly that was introduced but usingdeleteRealmIfMigrationNeeded
is actually the correct way afaik now.
– floriankrueger
Nov 4 '16 at 11:23
1
To the top! This is the correct answer!
– Alex Bartiş
Mar 12 '17 at 8:57
add a comment |
RealmSwift: Simple reset using a flag
Tried the above answers, but posting one more simple way to delete the realm file instead of migrating:
Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true
This simply sets a flag so that Realm can delete the existing file rather than crash on try! Realm()
Instead of manually deleting the file
Thought that was simpler than the Swift version of the answer above:
guard let path = Realm.Configuration.defaultConfiguration.fileURL?.absoluteString else
fatalError("no realm path")
do
try NSFileManager().removeItemAtPath(path)
catch
fatalError("couldn't remove at path")
RealmSwift: Simple reset using a flag
Tried the above answers, but posting one more simple way to delete the realm file instead of migrating:
Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true
This simply sets a flag so that Realm can delete the existing file rather than crash on try! Realm()
Instead of manually deleting the file
Thought that was simpler than the Swift version of the answer above:
guard let path = Realm.Configuration.defaultConfiguration.fileURL?.absoluteString else
fatalError("no realm path")
do
try NSFileManager().removeItemAtPath(path)
catch
fatalError("couldn't remove at path")
answered Nov 3 '16 at 17:26
jonchoijonchoi
10114
10114
I don't know in which version exactly that was introduced but usingdeleteRealmIfMigrationNeeded
is actually the correct way afaik now.
– floriankrueger
Nov 4 '16 at 11:23
1
To the top! This is the correct answer!
– Alex Bartiş
Mar 12 '17 at 8:57
add a comment |
I don't know in which version exactly that was introduced but usingdeleteRealmIfMigrationNeeded
is actually the correct way afaik now.
– floriankrueger
Nov 4 '16 at 11:23
1
To the top! This is the correct answer!
– Alex Bartiş
Mar 12 '17 at 8:57
I don't know in which version exactly that was introduced but using
deleteRealmIfMigrationNeeded
is actually the correct way afaik now.– floriankrueger
Nov 4 '16 at 11:23
I don't know in which version exactly that was introduced but using
deleteRealmIfMigrationNeeded
is actually the correct way afaik now.– floriankrueger
Nov 4 '16 at 11:23
1
1
To the top! This is the correct answer!
– Alex Bartiş
Mar 12 '17 at 8:57
To the top! This is the correct answer!
– Alex Bartiş
Mar 12 '17 at 8:57
add a comment |
In case someone stumbles on this question looking for a way to do this in Swift, this is just
DonamiteIsTnt's answer rewritten. I've added this function to a custom utility class so I can do MyAppUtilities.purgeRealm()
during testing & debugging
func purgeRealm()
NSFileManager.defaultManager().removeItemAtPath(RLMRealm.defaultRealmPath(), error: nil)
Note: If you find yourself in need of clearing data you might just check out Realm's new realm.addOrUpdateObject()
feature which allows you to replace existing data by its primary key with the new data. This way you're not continually adding new data. Just replacing "old" data. If you do use addOrUpdateObject()
make sure you override your model's primaryKey
class function so Realm knows which property is your primary key. In Swift, for example:
override class func primaryKey() -> String
return "_id"
add a comment |
In case someone stumbles on this question looking for a way to do this in Swift, this is just
DonamiteIsTnt's answer rewritten. I've added this function to a custom utility class so I can do MyAppUtilities.purgeRealm()
during testing & debugging
func purgeRealm()
NSFileManager.defaultManager().removeItemAtPath(RLMRealm.defaultRealmPath(), error: nil)
Note: If you find yourself in need of clearing data you might just check out Realm's new realm.addOrUpdateObject()
feature which allows you to replace existing data by its primary key with the new data. This way you're not continually adding new data. Just replacing "old" data. If you do use addOrUpdateObject()
make sure you override your model's primaryKey
class function so Realm knows which property is your primary key. In Swift, for example:
override class func primaryKey() -> String
return "_id"
add a comment |
In case someone stumbles on this question looking for a way to do this in Swift, this is just
DonamiteIsTnt's answer rewritten. I've added this function to a custom utility class so I can do MyAppUtilities.purgeRealm()
during testing & debugging
func purgeRealm()
NSFileManager.defaultManager().removeItemAtPath(RLMRealm.defaultRealmPath(), error: nil)
Note: If you find yourself in need of clearing data you might just check out Realm's new realm.addOrUpdateObject()
feature which allows you to replace existing data by its primary key with the new data. This way you're not continually adding new data. Just replacing "old" data. If you do use addOrUpdateObject()
make sure you override your model's primaryKey
class function so Realm knows which property is your primary key. In Swift, for example:
override class func primaryKey() -> String
return "_id"
In case someone stumbles on this question looking for a way to do this in Swift, this is just
DonamiteIsTnt's answer rewritten. I've added this function to a custom utility class so I can do MyAppUtilities.purgeRealm()
during testing & debugging
func purgeRealm()
NSFileManager.defaultManager().removeItemAtPath(RLMRealm.defaultRealmPath(), error: nil)
Note: If you find yourself in need of clearing data you might just check out Realm's new realm.addOrUpdateObject()
feature which allows you to replace existing data by its primary key with the new data. This way you're not continually adding new data. Just replacing "old" data. If you do use addOrUpdateObject()
make sure you override your model's primaryKey
class function so Realm knows which property is your primary key. In Swift, for example:
override class func primaryKey() -> String
return "_id"
edited Oct 9 '14 at 20:57
answered Oct 9 '14 at 20:48
Glen SelleGlen Selle
3,23743056
3,23743056
add a comment |
add a comment |
I ran into this fun little issue. So instead i queried the schema version directly before running the schemamigration.
NSError *error = NULL;
NSUInteger currentSchemaVersion = [RLMRealm schemaVersionAtPath:[RLMRealm defaultRealmPath] error:&error];
if (currentSchemaVersion == RLMNotVersioned)
// new db, skip
else if (currentSchemaVersion < 26)
// kill local db
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:&error];
if (error)
MRLogError(error);
else if (error)
// for good measure...
MRLogError(error);
// perform realm migration
[RLMRealm setSchemaVersion:26
forRealmAtPath:[RLMRealm defaultRealmPath]
withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion)
];
nice idea, I'm definately going to try that :) thank you!
– floriankrueger
May 28 '15 at 5:24
add a comment |
I ran into this fun little issue. So instead i queried the schema version directly before running the schemamigration.
NSError *error = NULL;
NSUInteger currentSchemaVersion = [RLMRealm schemaVersionAtPath:[RLMRealm defaultRealmPath] error:&error];
if (currentSchemaVersion == RLMNotVersioned)
// new db, skip
else if (currentSchemaVersion < 26)
// kill local db
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:&error];
if (error)
MRLogError(error);
else if (error)
// for good measure...
MRLogError(error);
// perform realm migration
[RLMRealm setSchemaVersion:26
forRealmAtPath:[RLMRealm defaultRealmPath]
withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion)
];
nice idea, I'm definately going to try that :) thank you!
– floriankrueger
May 28 '15 at 5:24
add a comment |
I ran into this fun little issue. So instead i queried the schema version directly before running the schemamigration.
NSError *error = NULL;
NSUInteger currentSchemaVersion = [RLMRealm schemaVersionAtPath:[RLMRealm defaultRealmPath] error:&error];
if (currentSchemaVersion == RLMNotVersioned)
// new db, skip
else if (currentSchemaVersion < 26)
// kill local db
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:&error];
if (error)
MRLogError(error);
else if (error)
// for good measure...
MRLogError(error);
// perform realm migration
[RLMRealm setSchemaVersion:26
forRealmAtPath:[RLMRealm defaultRealmPath]
withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion)
];
I ran into this fun little issue. So instead i queried the schema version directly before running the schemamigration.
NSError *error = NULL;
NSUInteger currentSchemaVersion = [RLMRealm schemaVersionAtPath:[RLMRealm defaultRealmPath] error:&error];
if (currentSchemaVersion == RLMNotVersioned)
// new db, skip
else if (currentSchemaVersion < 26)
// kill local db
[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:&error];
if (error)
MRLogError(error);
else if (error)
// for good measure...
MRLogError(error);
// perform realm migration
[RLMRealm setSchemaVersion:26
forRealmAtPath:[RLMRealm defaultRealmPath]
withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion)
];
answered May 27 '15 at 12:26
Steve PascoeSteve Pascoe
17628
17628
nice idea, I'm definately going to try that :) thank you!
– floriankrueger
May 28 '15 at 5:24
add a comment |
nice idea, I'm definately going to try that :) thank you!
– floriankrueger
May 28 '15 at 5:24
nice idea, I'm definately going to try that :) thank you!
– floriankrueger
May 28 '15 at 5:24
nice idea, I'm definately going to try that :) thank you!
– floriankrueger
May 28 '15 at 5:24
add a comment |
You can also go to the location where your realm file is stored, delete that file from there and next time when you open realm after running app, you will see the empty realm database in browser.
add a comment |
You can also go to the location where your realm file is stored, delete that file from there and next time when you open realm after running app, you will see the empty realm database in browser.
add a comment |
You can also go to the location where your realm file is stored, delete that file from there and next time when you open realm after running app, you will see the empty realm database in browser.
You can also go to the location where your realm file is stored, delete that file from there and next time when you open realm after running app, you will see the empty realm database in browser.
answered Mar 7 at 12:26
Prathma RastogiPrathma Rastogi
93
93
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26067193%2fclear-complete-realm-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown