2

How can I delete the Room database of my app? I've found the clearAllTables() method, but that's not what I want. Instead I really want to delete the database on user logout and start all over.

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

1 Answers1

4

Step #1: Call close() on your RoomDatabase. That should coalesce the contents of the wal and shm files into the main SQLite database file, leaving you only one file to delete.

Step #2: Delete that database file. If you stored it in the default location, you can call deleteDatabase() on a Context.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I was deleting the three files before to do that, the `db.close()` is really neat, and just the database file get touched. Thanks so much! – Zain May 20 '23 at 21:55
  • 1
    @Zain: Yes, always `close()` the database before trying to manipulate the files, such as making copies for backups or exports, deleting, etc. That way, all the "write-ahead logging" data gets put into the main database. For deleting, deleting the three files certainly works, but for other scenarios, such as copying, you are much better off with just the single file. – CommonsWare May 20 '23 at 22:18