Skip to content

Instantly share code, notes, and snippets.

@dblodgett-usgs
Created October 3, 2022 22:14
Show Gist options
  • Save dblodgett-usgs/61425b5cb58bd570a47e7ed054a8d7b5 to your computer and use it in GitHub Desktop.
Save dblodgett-usgs/61425b5cb58bd570a47e7ed054a8d7b5 to your computer and use it in GitHub Desktop.
remove gpkg layer with R using RSQLite
remove_gpkg_table <- function(db, table) {
con <- RSQLite::dbConnect(RSQLite::SQLite(), db)
on.exit(RSQLite::dbDisconnect(con))
o <- RSQLite::dbRemoveTable(con, table)
o <- RSQLite::dbSendQuery(con, sprintf("DELETE FROM gpkg_contents where table_name='%s';", table))
RSQLite::dbClearResult(o)
}
@rouault
Copy link

rouault commented Oct 6, 2022

This is not enough in the general case to remove all references to the deleted table.
Calling GDALDataset::DeleteLayer(layer_index) or GDALDataset::ExecuteSQL("DROP TABLE table_name") would make sure every reference to the deleted layer in auxiliary gpkg_ tables are removed. Note that the "DROP TABLE table_name" is intercepted in the GPKG driver to do more than just forwarding it to SQLite: https://github.com/OSGeo/gdal/blob/master/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp#L6362

@dblodgett-usgs
Copy link
Author

Thanks @rouault -- I opened an issue in sf to ask about it. The above is a hack I used in some tests -- not planning on putting this into anything that matters!

@edzer
Copy link

edzer commented Oct 6, 2022

For completeness: sf::st_delete() does this.

@dblodgett-usgs
Copy link
Author

lol -- I've searched for this a number of times and totally missed that st_delete exists. Thanks @edzer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment