0

This seems like it ought to be straightforward:

from osgeo import gdal
gpkg = gdal.OpenEx('path_to_gpk',gdal.OF_VERBOSE_ERROR,allowed_drivers=['GPKG'])
error = gpkg.DeleteLayer(0)

After running this error is always a OGRERR_FAILURE. The geopackage has been created by QGIS so I would have thought should be standard.

Can't see any way to debug further into the GDAL calls apart from a GDB session, which I might try later.

1

1 Answer 1

1

Simple indeed....

Proper code below:

from osgeo import gdal
gpkg = gdal.OpenEx('path_to_gpk',gdal.OF_UPDATE,allowed_drivers=['GPKG'])
error = gpkg.DeleteLayer(0)
# above works with gdal.__version__ 3.4.2 without VACUUM call
# this code is also possible as an alternative
# doesn't seem to delete everything though, so possibly DeleteLayer() is better
# gpkg.ExecuteSQL('DROP TABLE [layer name]')
gpkg.ExecuteSQL('VACUUM')

Note that you will have to refresh any connections (e.g. in QGIS).

1
  • What remains after DROP TABLE? It certainly works when it is used as -sql parameter with ogrinfo.
    – user30184
    Commented Oct 17, 2019 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.