Programmatically I have added the vector layers to an existing GeoPackage in QGIS 3.2 using Python. Similarly, I would like to delete a layer from the existing GeoPackage using Python code. But there is no example/help found online.

Here is my code to add the selected layers to an existing Geo package:

layer = self.iface.activeLayer() 
options = QgsVectorFileWriter.SaveVectorOptions()
options.actionOnExistingFile =QgsVectorFileWriter.CreateOrOverwriteLayer   
options.layerName = 'test'

enter image description here

  • 2
    There is quite a lot to do when a table is totally and properly dropped from GeoPackage (drop spatial index, drop a bunch of triggers, remove outdated rows from metadata tables, and finally drop the table itself). If QGIS does not have a dedicated method for that (I don't know if it does) the easiest alternative is probably to let GDAL to drop the table with SQL DROP TABLE layer_name as documented in gdal.org/drv_geopackage.html. – user30184 Apr 24 '19 at 18:43

You can use the SpatiaLite execute SQL tool and drop table:

gpkg = '/home/bera/GIS/Data/testdata/overlaps.gpkg'
lyr = 'overlaps'

processing.run("native:spatialiteexecutesql", {'DATABASE':'{0}|layername={1}'.format(gpkg, lyr),
    'SQL':'drop table {0}'.format(lyr)})

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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