QGIS is made to use Write Ahead Logging (WAL) by commit https://github.com/qgis/QGIS/commit/f939e9cff598b95e95b0de099d0c9a92eed0ea9c
The new behavior should be to open gpkg database file in WAL journal mode if file is on local disk but turn it back to the default journal mode when the connection is closed.
I made a test with QGIS 2.18.2 and that seems to work. I opened a gpkg database with "Add Vector Layer" and immediately -wal and -shm files appear in the same directory. When I removed the layer from QGIS both files disappear. I verified with spatialite-gui that the journal mode was really turned back by making a pragma request
You must not ever delete manually -wal and -shm files. If they still are there in the directory I would check if there are any other clients than QGIS having connections. If not, opening and closing db with QGIS may clear the temporary files and turn logging to default mode. If that does not help, try with the pragma command
It is not possible to turn the journal mode from WAL to anything else if there are any other open connections. Only the last connection can do the change. Also, journal mode is persistent and gpkg database that is set to WAL mode remains in WAL mode until it is explicitly set to something else. I guess that this may cause troubles sometimes. I made this simple test:
- Open a gpkg that is originally in journal_mode=delete with QGIS -> journal mode turns into WAL
- Open the same gpkg with spatialite-gui -> journal mode is still WAL
- Close the layer from QGIS -> journal mode is WAL in spatialite-gui
- Close gpkg from spatialite-gui -> the -wal and -shm files disappear but journal mode remains WAL
In this state the gpkg database will work from local disk but not from network drives if copies of it are given to end users!
These are the two ways reported by Even Roualt for turning WAL mode off from QGIS:
People can either define the OGR_SQLITE_JOURNAL environment variable
to DELETE or set the QGIS setting "/qgis/walForSqlite3" (in advanced
mode) to false, and this will prevent QGIS from enabling WAL on
opening. The drawback is potential deadlocks in some situations where
a reader and writer would run concurrently.