[QGIS-Developer] Unload QgsVectorLayer/QgsrasterLayer

Bourdon, Jean-François (DIF) Jean-Francois.Bourdon at mffp.gouv.qc.ca
Mon Aug 10 13:37:44 PDT 2020


Hi list,

I have a very simple problem: I can't delete a file after loading it with QgsVectorLayer() or QgsRasterLayer() within a script (custom Processing Provider) as I get the error saying the file is still in use. My code to replicate:

path_raster = "C:/temp/raster.tif"
rlayer = QgsRasterLayer(path_raster, "flow")
QgsProject.instance().addMapLayer(rlayer, False)
QgsProject.instance().removeMapLayer(rlayer.id())
os.remove(path_raster)

However, the same code directly put inside the Python Console within QGIS works, but it won't if used inside a Processing Provider plugin. I also tried using del rlayer without success. There is several questions about releasing QGIS' lock on files on GIS StackExchange (e.g. here<https://gis.stackexchange.com/questions/243520/deleting-raster-file-after-doing-some-processing-in-qgis> and here<https://gis.stackexchange.com/questions/77218/releasing-pyqgis-file-locks>) but it doesn't seem to work for everyone. Maybe it was working in version 2.X, but it fails on 3.12 (Windows 10).

Many thanks

Jean-François Bourdon, ing.f.
Analyste en télédétection
Direction des inventaires forestiers
Ministère des Forêts, de la Faune et des Parcs
5700, 4e Avenue Ouest, local A-108
Québec (Québec) G1H 6R1
Téléphone : 418 627-8669, poste 4304
jean-francois.bourdon at mffp.gouv.qc.ca
mffp.gouv.qc.ca<http://www.mffp.gouv.qc.ca/accueil.jsp>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200810/34da67d5/attachment.html>


More information about the QGIS-Developer mailing list