Skip to main content
added 34 characters in body; edited tags; edited title
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

How to delete gpkg Deleting GeoPackage vector layers and releasereleasing open file handles with python apiPyQGIS

This is inOn Windows 11 QGIS

I have a pyqgisPyQGIS script that creates layers from gpkg files. This This all works perfectly fine.

I want to delete these layers, recreate the gpkg files and reload them as layers again and this is where it comes unstuck.

LetsLet's say I have 2two gpkg layers in my project, layer1.gpkglayer1.gpkg and layer2layer2.gpkg.gpkg

When I open the QGIS project, I see in the windows file handles, there are 2 file handles open for each layer file.

If I manually remove layer1 from the project (right click - remove layer) and then check the file handles, oddly I will be left with just a single open file handle for layer2layer2.gpkg.gpkg

If I then use the script to add layer1.gpkglayer1.gpkg and check the file handles, I am back to 2 file handles open for each layer.

If I then delete layer1 using my script (and leave it running), I see 2 file handles for layer2 and 1 file handle for layer1 left open.

This remaining open file handle for layer1.gpkglayer1.gpkg stops me over-writingfrom overwriting the file, and that is my problem.

I am removing layer1 like this:

QgsProject.instance().removeMapLayer(layer1.layerId())
QgsProject.instance().reloadAllLayers()
iface.mapCanvas().refresh() # Just in case
gc.collect() # Just in case
QgsProject.instance().removeMapLayer(layer1.layerId())
QgsProject.instance().reloadAllLayers()
iface.mapCanvas().refresh() # Just in case
gc.collect() # Just in case

This removes the layer in the tree, but leaves one open file handle on the file. As As opposed to removing the file using the "Remove Layer" menu item, which leaves no file handles open on the file.

The only thing I see in the API documentation is the removeMapLayer marks the project dirty, but it seems there must be some other housekeeping that needs to be done to release that other file handle from the OS.

How to delete gpkg vector layers and release open file handles with python api

This is in Windows 11 QGIS

I have a pyqgis script that creates layers from gpkg files. This all works perfectly fine.

I want to delete these layers, recreate the gpkg files and reload them as layers again and this is where it comes unstuck.

Lets say I have 2 gpkg layers in my project, layer1.gpkg and layer2.gpkg

When I open the QGIS project, I see in the windows file handles, there are 2 file handles open for each layer file.

If I manually remove layer1 from the project (right click - remove layer) and then check the file handles, oddly I will be left with just a single open file handle for layer2.gpkg

If I then use the script to add layer1.gpkg and check the file handles, I am back to 2 file handles open for each layer.

If I then delete layer1 using my script (and leave it running), I see 2 file handles for layer2 and 1 file handle for layer1 left open.

This remaining open file handle for layer1.gpkg stops me over-writing the file, and that is my problem.

I am removing layer1 like this

QgsProject.instance().removeMapLayer(layer1.layerId())
QgsProject.instance().reloadAllLayers()
iface.mapCanvas().refresh() # Just in case
gc.collect() # Just in case

This removes the layer in the tree, but leaves one open file handle on the file. As opposed to removing the file using "Remove Layer" menu item, which leaves no file handles open on the file.

The only thing I see in the API documentation is the removeMapLayer marks the project dirty, but it seems there must be some other housekeeping that needs to be done to release that other file handle from the OS.

Deleting GeoPackage vector layers and releasing open file handles with PyQGIS

On Windows 11 I have a PyQGIS script that creates layers from gpkg files. This all works perfectly fine.

I want to delete these layers, recreate the gpkg files and reload them as layers again and this is where it comes unstuck.

Let's say I have two gpkg layers in my project, layer1.gpkg and layer2.gpkg.

When I open the QGIS project, I see in the windows file handles, there are 2 file handles open for each layer file.

If I manually remove layer1 from the project (right click - remove layer) and then check the file handles, oddly I will be left with just a single open file handle for layer2.gpkg.

If I then use the script to add layer1.gpkg and check the file handles, I am back to 2 file handles open for each layer.

If I then delete layer1 using my script (and leave it running), I see 2 file handles for layer2 and 1 file handle for layer1 left open.

This remaining open file handle for layer1.gpkg stops me from overwriting the file, and that is my problem.

I am removing layer1 like this:

QgsProject.instance().removeMapLayer(layer1.layerId())
QgsProject.instance().reloadAllLayers()
iface.mapCanvas().refresh() # Just in case
gc.collect() # Just in case

This removes the layer in the tree but leaves one open file handle on the file. As opposed to removing the file using the "Remove Layer" menu item, which leaves no file handles open on the file.

The only thing I see in the API documentation is the removeMapLayer marks the project dirty, but it seems there must be some other housekeeping that needs to be done to release that other file handle from the OS.

edited tags
Link
Ian Turton
  • 83.2k
  • 6
  • 89
  • 189
deleted 21 characters in body; edited tags
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

This is in Windows 11 QGIS

I have a pyqgis script that creates layers from gpkg files. This all works perfectly fine.

I want to delete these layers, recreate the gpkg files and reload them as layers again and this is where it comes unstuck.

Lets say I have 2 gpkg layers in my project, layer1.gpkg and layer2.gpkg

When I open the QGIS project, I see in the windows file handles, there are 2 file handles open for each layer file.

If I manually remove layer1 from the project (right click - remove layer) and then check the file handles, oddly I will be left with just a single open file handle for layer2.gpkg

If I then use the script to add layer1.gpkg and check the file handles, I am back to 2 file handles open for each layer.

If I then delete layer1 using my script (and leave it running), I see 2 file handles for layer2 and 1 file handle for layer1 left open.

This remaining open file handle for layer1.gpkg stops me over-writing the file, and that is my problem.

I am removing layer1 like this

QgsProject.instance().removeMapLayer(layer1.layerId())
QgsProject.instance().reloadAllLayers()
iface.mapCanvas().refresh() # Just in case
gc.collect() # Just in case

This removes the layer in the tree, but leaves one open file handle on the file. As opposed to removing the file using "Remove Layer" menu item, which leaves no file handles open on the file.

The only thing I see in the API documentation is the removeMapLayer marks the project dirty, but it seems there must be some other housekeeping that needs to be done to release that other file handle from the OS.

Thanks in advance

This is in Windows 11 QGIS

I have a pyqgis script that creates layers from gpkg files. This all works perfectly fine.

I want to delete these layers, recreate the gpkg files and reload them as layers again and this is where it comes unstuck.

Lets say I have 2 gpkg layers in my project, layer1.gpkg and layer2.gpkg

When I open the QGIS project, I see in the windows file handles, there are 2 file handles open for each layer file.

If I manually remove layer1 from the project (right click - remove layer) and then check the file handles, oddly I will be left with just a single open file handle for layer2.gpkg

If I then use the script to add layer1.gpkg and check the file handles, I am back to 2 file handles open for each layer.

If I then delete layer1 using my script (and leave it running), I see 2 file handles for layer2 and 1 file handle for layer1 left open.

This remaining open file handle for layer1.gpkg stops me over-writing the file, and that is my problem.

I am removing layer1 like this

QgsProject.instance().removeMapLayer(layer1.layerId())
QgsProject.instance().reloadAllLayers()
iface.mapCanvas().refresh() # Just in case
gc.collect() # Just in case

This removes the layer in the tree, but leaves one open file handle on the file. As opposed to removing the file using "Remove Layer" menu item, which leaves no file handles open on the file.

The only thing I see in the API documentation is the removeMapLayer marks the project dirty, but it seems there must be some other housekeeping that needs to be done to release that other file handle from the OS.

Thanks in advance

This is in Windows 11 QGIS

I have a pyqgis script that creates layers from gpkg files. This all works perfectly fine.

I want to delete these layers, recreate the gpkg files and reload them as layers again and this is where it comes unstuck.

Lets say I have 2 gpkg layers in my project, layer1.gpkg and layer2.gpkg

When I open the QGIS project, I see in the windows file handles, there are 2 file handles open for each layer file.

If I manually remove layer1 from the project (right click - remove layer) and then check the file handles, oddly I will be left with just a single open file handle for layer2.gpkg

If I then use the script to add layer1.gpkg and check the file handles, I am back to 2 file handles open for each layer.

If I then delete layer1 using my script (and leave it running), I see 2 file handles for layer2 and 1 file handle for layer1 left open.

This remaining open file handle for layer1.gpkg stops me over-writing the file, and that is my problem.

I am removing layer1 like this

QgsProject.instance().removeMapLayer(layer1.layerId())
QgsProject.instance().reloadAllLayers()
iface.mapCanvas().refresh() # Just in case
gc.collect() # Just in case

This removes the layer in the tree, but leaves one open file handle on the file. As opposed to removing the file using "Remove Layer" menu item, which leaves no file handles open on the file.

The only thing I see in the API documentation is the removeMapLayer marks the project dirty, but it seems there must be some other housekeeping that needs to be done to release that other file handle from the OS.

Source Link
Loading