Nozbe / WatermelonDB

πŸ‰ Reactive & asynchronous database for powerful React and React Native apps ⚑️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

How to delete all the records from the table #211

Closed skhizerabass closed 5 years ago

skhizerabass commented 5 years ago

I want to clear all the records from different tables. Is there anyway to do that ?

radex commented 5 years ago

You can clear the whole database using database.unsafeClearDatabase(). There's no feature for cleaing the whole table. You can delete contents of an empty query collection.query().markAllAsDeleted() (or destroyAllPermanently()), but it's not very efficient (yet). You could also use a raw query β€” see https://github.com/Nozbe/WatermelonDB/issues/103 for discussion

skhizerabass commented 5 years ago

Thank you very much . This is the exact thing i wanted . I wanted to clear my db and it worked like a charm

cesargdm commented 5 years ago

I see that the method has changed to database.unsafeResetDatabase(). Could It be documented? @radex

radex commented 5 years ago

it should! I remember there was an issue where this was explained more clearly β€”can you find it @cesargdm ?

luk3skyw4lker commented 3 years ago

If I use the unsafeResetDatabase does my sync timestamp resets to null too?

mohamedanwer123 commented 3 years ago

change version of appSchema and reload the app .. this way will clear all database :) πŸ‘

const schemas = appSchema({ version: 1, //make it version: 2 })

KrisLau commented 1 year ago

@luk3skyw4lker did you ever find out the answer to that?

luk3skyw4lker commented 1 year ago

@KrisLau I think that yes, the sync timestamp was also set to null. It's been a long time since my last contact with the library, but I'm almost sure that the timestamp was set to null too.

ushicode commented 7 months ago

This works fine for me;

async function resetDatabase() {
    try {
      await database.action(async () => {
        await database.unsafeResetDatabase();
      });
    } catch (error: any) {
      console.log('Error clearing local database', error);
    }
  }
KrisLau commented 6 months ago

@ushicode do you use that for migrations? I'm trying to reset my database and then sync again immediately but I'm having trouble figuring out how to do so: https://github.com/Nozbe/WatermelonDB/issues/1609