I'm using SQLite.swift library in my iOS app for database needs. I'm using more than one database with different names. I'm doing it fine no problems. At one point I need to delete the .sqlite3
file i.e., the whole database file when it's no longer needed. I'm a newbie to Swift3
. So I don't know how to delete the database. My app has a database called AccountsDB
to store the number of profiles. Each profile has a database with it's name. When user deletes one profile I need to delete the database with that name too. This is my scenario. Please guide me to achieve my task. Thanks.
Asked
Active
Viewed 3,184 times
0

Praveen Kumar
- 547
- 1
- 7
- 33
-
Are you sure that you want to delete the `sqlite3` file and not the table ? – Nirav D Mar 24 '17 at 04:30
-
@NiravD yes, I no longer needed that database. – Praveen Kumar Mar 24 '17 at 04:33
-
1@NiravD let me try. thanks. – Praveen Kumar Mar 24 '17 at 04:36
-
1Please note that there might be `AccountsDB.sqlite3-journal`, `*-wal`, or `*-shm` files, which also need to be deleted. – CL. Mar 24 '17 at 08:31
1 Answers
1
Use FileManager
to delete the physical file from your device. I assume you know (or can retrieve) the path to the SQLite DB file, right? In that case, if url
is the file URL to your file:
let fm = FileManager.default
do {
try fm.removeItem(at:url)
} catch {
NSLog("Error deleting file: \(url)")
}

Fahim
- 3,466
- 1
- 12
- 18