As of Room 1.1. 0 you can use clearAllTables() which "deletes all rows from all the tables that are registered to this database as entities()." I've included this as an answer below, but am reproducing here for visibility.
But, you can go to yourApp -> Long click -> App Info -> Storage & Cache -> clear both cache and Storage. Clearing storage displays a verification dialog that indicates that databases will be destroyed.
The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. In particular, Room provides the following benefits: Compile-time verification of SQL queries.
You can create a DAO method to do this.
@Dao
interface MyDao {
@Query("DELETE FROM myTableName")
public void nukeTable();
}
As of Room 1.1.0
you can use clearAllTables() which:
Deletes all rows from all the tables that are registered to this database as entities().
If want to delete an entry from the the table in Room simply call this function,
@Dao
public interface myDao{
@Delete
void delete(MyModel model);
}
Update: And if you want to delete complete table, call below function,
@Query("DELETE FROM MyModel")
void delete();
Note: Here MyModel is a Table Name.
Use clearAllTables() with RXJava like below inorder to avoid java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
getRoomDatabase().clearAllTables();
}
}).subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribe(new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "--- clearAllTables(): run() ---");
getInteractor().setUserAsLoggedOut();
getMvpView().openLoginActivity();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.d(TAG, "--- clearAllTables(): accept(Throwable throwable) ----");
Log.d(TAG, "throwable.getMessage(): "+throwable.getMessage());
}
});
I had issues with delete all method when using RxJava to execute this task on background. This is how I finally solved it:
@Dao
interface UserDao {
@Query("DELETE FROM User")
fun deleteAll()
}
and
fun deleteAllUsers() {
return Maybe.fromAction(userDao::deleteAll)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe ({
d("database rows cleared: $it")
}, {
e(it)
}).addTo(compositeDisposable)
}
This is how we do it from a Fragment.
fun Fragment.emptyDatabase() {
viewLifecycleOwner.lifecycleScope.launchWhenCreated {
withContext(Dispatchers.IO) {
Database.getInstance(requireActivity()).clearAllTables()
}
}
}
If you are emptying the database from an activity use this:
fun Activity.emptyDatabase() {
// create a scope to access the database from a thread other than the main thread
val scope = CoroutineScope(Dispatchers.Default)
scope.launch {
SitukaDatabase.getInstance(this@emptyDatabase).clearAllTables()
}
}
It could also be possible to call the clearAllTables
method from the main thread. I haven't tried it out but I noticed that Android Studio does not recognize the call as a suspend function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With