<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="FR-CA" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi list,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-CA">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:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA" style="font-size:10.0pt;font-family:"Courier New"">path_raster = "C:/temp/raster.tif"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA" style="font-size:10.0pt;font-family:"Courier New"">rlayer = QgsRasterLayer(path_raster, "flow")<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA" style="font-size:10.0pt;font-family:"Courier New"">QgsProject.instance().addMapLayer(rlayer, False)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA" style="font-size:10.0pt;font-family:"Courier New"">QgsProject.instance().removeMapLayer(rlayer.id())  
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA" style="font-size:10.0pt;font-family:"Courier New"">os.remove(path_raster)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA">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
</span><span lang="EN-CA" style="font-size:10.0pt;font-family:"Courier New"">del rlayer</span><span lang="EN-CA"> without success. There is several questions about releasing QGIS’ lock on files on GIS StackExchange (e.g.
<a href="https://gis.stackexchange.com/questions/243520/deleting-raster-file-after-doing-some-processing-in-qgis">
here</a> and <a href="https://gis.stackexchange.com/questions/77218/releasing-pyqgis-file-locks">
here</a>) 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).
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-CA">Many thanks<o:p></o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto"><span lang="EN-CA" style="color:black;mso-fareast-language:FR-CA"><o:p> </o:p></span></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto"><b><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black;mso-fareast-language:FR-CA">Jean-François Bourdon, ing.f.</span></b><span style="font-size:9.0pt;font-family:"Arial",sans-serif;color:black;mso-fareast-language:FR-CA"><o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#2E74B5;mso-fareast-language:FR-CA">Analyste en télédétection</span></b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;mso-fareast-language:FR-CA"><br>
<b><span style="color:#2E74B5">Direction des inventaires forestiers</span></b><br>
<b><span style="color:#2E74B5">Ministère des Forêts, de la Faune et des Parcs</span></b><br>
<span style="color:#2E74B5">5700, 4<sup>e</sup> Avenue Ouest, local A-108</span><br>
<span style="color:#2E74B5">Québec (Québec) G1H 6R1</span><br>
<span style="color:#2E74B5">Téléphone : 418 627-8669, poste 4304</span><br>
<b><u><span style="color:#2E74B5">jean-francois.bourdon@mffp.gouv.qc.ca</span></u></b>
<br>
</span><span style="mso-fareast-language:FR-CA"><a href="http://www.mffp.gouv.qc.ca/accueil.jsp"><b><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#2E74B5">mffp.gouv.qc.ca</span></b></a></span><span style="mso-fareast-language:FR-CA"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>