Skip to main content
added 257 characters in body
Source Link

Turns out QgsVectorFileWriter.ActionOnExistingFile.CreateOrOverwriteFile doesn't work on Windows, when the file is opened in QGIS. So the .shp-file and the .dbf-file can't be deleted. Admitedly the error-message from QGIS is really not of any help here.

The final "solution" was to delete the features within the shapefile before deleting the shapefile itself, as mentioned in this topic:

root = QgsProject.instance().layerTreeRoot() groupLayer = root.findGroup(resultLayerName) if groupLayer: for child in groupLayer.children(): if isinstance(child, QgsLayerTreeLayer): provider = child.layer().dataProvider() #deleting all features in the Vector layer provider.truncate() QgsProject.instance().removeMapLayer(child.layerId()) QgsVectorFileWriter.deleteShapeFile(child.source()) root.removeChildNode(groupLayer) 

As writeAsVectorFormatV3 will call deleteShapeFile , when the option CreateOrOverwriteFile is set, we won't need to call the latter ourselves, so I omitted that step here. The important thing here is just to call truncate on the dataprovider in order to empty the layer. This eventually releases the lock from the layer when we remove it from the map.

Turns out QgsVectorFileWriter.ActionOnExistingFile.CreateOrOverwriteFile doesn't work on Windows, when the file is opened in QGIS. So the .shp-file and the .dbf-file can't be deleted. Admitedly the error-message from QGIS is really not of any help here.

The final "solution" was to delete the features within the shapefile before deleting the shapefile itself, as mentioned in this topic:

root = QgsProject.instance().layerTreeRoot() groupLayer = root.findGroup(resultLayerName) if groupLayer: for child in groupLayer.children(): if isinstance(child, QgsLayerTreeLayer): provider = child.layer().dataProvider() #deleting all features in the Vector layer provider.truncate() QgsProject.instance().removeMapLayer(child.layerId()) QgsVectorFileWriter.deleteShapeFile(child.source()) root.removeChildNode(groupLayer) 

Turns out QgsVectorFileWriter.ActionOnExistingFile.CreateOrOverwriteFile doesn't work on Windows, when the file is opened in QGIS. So the .shp-file and the .dbf-file can't be deleted. Admitedly the error-message from QGIS is really not of any help here.

The final "solution" was to delete the features within the shapefile, as mentioned in this topic:

root = QgsProject.instance().layerTreeRoot() groupLayer = root.findGroup(resultLayerName) if groupLayer: for child in groupLayer.children(): if isinstance(child, QgsLayerTreeLayer): provider = child.layer().dataProvider() #deleting all features in the Vector layer provider.truncate() QgsProject.instance().removeMapLayer(child.layerId()) root.removeChildNode(groupLayer) 

As writeAsVectorFormatV3 will call deleteShapeFile , when the option CreateOrOverwriteFile is set, we won't need to call the latter ourselves, so I omitted that step here. The important thing here is just to call truncate on the dataprovider in order to empty the layer. This eventually releases the lock from the layer when we remove it from the map.

edited body
Source Link

Turns out QgsVectorFileWriter.ActionOnExistingFile.CreateOrOverwriteFile doesn't work on Windows, when the file is opened in QGIS. So the .shp-file and the .dbf-file can't be deleted. Admitedly the error-message from QGIS is really not of any help here.

The final "solution" was to delete the features within the shapefile before deleting the shapefile itself, as mentioned in this topic:

root = QgsProject.instance().layerTreeRoot() groupLayer = root.findGroup(resultLayerName) if groupLayer: for child in groupLayer.children(): if isinstance(child, QgsLayerTreeLayer): provider = child.layer().dataProvider() #deleting all features in the Vector layer provider.truncate() QgsProject.instance().removeMapLayer(child.layerId()) QgsVectorFileWriter.deleteShapeFile(layerchild.source()) root.removeChildNode(groupLayer) 

Turns out QgsVectorFileWriter.ActionOnExistingFile.CreateOrOverwriteFile doesn't work on Windows, when the file is opened in QGIS. So the .shp-file and the .dbf-file can't be deleted. Admitedly the error-message from QGIS is really not of any help here.

The final "solution" was to delete the features within the shapefile before deleting the shapefile itself, as mentioned in this topic:

root = QgsProject.instance().layerTreeRoot() groupLayer = root.findGroup(resultLayerName) if groupLayer: for child in groupLayer.children(): if isinstance(child, QgsLayerTreeLayer): provider = child.layer().dataProvider() #deleting all features in the Vector layer provider.truncate() QgsProject.instance().removeMapLayer(child.layerId()) QgsVectorFileWriter.deleteShapeFile(layer.source()) root.removeChildNode(groupLayer) 

Turns out QgsVectorFileWriter.ActionOnExistingFile.CreateOrOverwriteFile doesn't work on Windows, when the file is opened in QGIS. So the .shp-file and the .dbf-file can't be deleted. Admitedly the error-message from QGIS is really not of any help here.

The final "solution" was to delete the features within the shapefile before deleting the shapefile itself, as mentioned in this topic:

root = QgsProject.instance().layerTreeRoot() groupLayer = root.findGroup(resultLayerName) if groupLayer: for child in groupLayer.children(): if isinstance(child, QgsLayerTreeLayer): provider = child.layer().dataProvider() #deleting all features in the Vector layer provider.truncate() QgsProject.instance().removeMapLayer(child.layerId()) QgsVectorFileWriter.deleteShapeFile(child.source()) root.removeChildNode(groupLayer) 
Source Link

Turns out QgsVectorFileWriter.ActionOnExistingFile.CreateOrOverwriteFile doesn't work on Windows, when the file is opened in QGIS. So the .shp-file and the .dbf-file can't be deleted. Admitedly the error-message from QGIS is really not of any help here.

The final "solution" was to delete the features within the shapefile before deleting the shapefile itself, as mentioned in this topic:

root = QgsProject.instance().layerTreeRoot() groupLayer = root.findGroup(resultLayerName) if groupLayer: for child in groupLayer.children(): if isinstance(child, QgsLayerTreeLayer): provider = child.layer().dataProvider() #deleting all features in the Vector layer provider.truncate() QgsProject.instance().removeMapLayer(child.layerId()) QgsVectorFileWriter.deleteShapeFile(layer.source()) root.removeChildNode(groupLayer)