<div dir="ltr"><div>Hi,<br></div><div><br></div><div>Are you on Windows ?</div><div><br></div><div>It's long-standing issue on Windows IIRC :</div><div><a href="https://gis.stackexchange.com/questions/365713/deleting-shapefile-python-lock-qgis-3-10">https://gis.stackexchange.com/questions/365713/deleting-shapefile-python-lock-qgis-3-10</a> or other links.</div><div><br></div><div>Maybe <br></div><div> lyr = QgsProject.instance().mapLayersByName(diff_layer)[0]</div><div>QgsProject.instance().removeMapLayer(<a href="http://lyr.id">lyr.id</a>())<br>lyr = None<br>del lyr<br></div><div><br></div><div>But not a real answer ...<br></div><div><br></div><div>FYI, a nice helper to know side car files:</div><div><a href="https://qgis.org/api/classQgsFileUtils.html#a8db232d5063ae13133fa2d3351be1b9c">https://qgis.org/api/classQgsFileUtils.html#a8db232d5063ae13133fa2d3351be1b9c</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le mar. 22 févr. 2022 à 18:07, pierluigi de rosa <<a href="mailto:pierluigi.derosa@gmail.com">pierluigi.derosa@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Dear all,<div><br></div><div>I have some troubles wher I use in a pyqgis the function</div><div>QgsVectorFileWriter.deleteShapeFile(file_path)</div><div>here below the example:</div><div><pre style="white-space:pre-wrap;background-color:rgb(43,43,43);color:rgb(169,183,198);font-family:"JetBrains Mono",monospace"><span style="color:rgb(204,120,50)">def </span><span style="color:rgb(255,198,109)">removeLayer</span>(path_layer):<br>    <span style="color:rgb(128,128,128)"># remove layer from TOC if already loaded<br></span><span style="color:rgb(128,128,128)">    </span>basefile = os.path.basename(path_layer)<br>    diff_layer = os.path.splitext(basefile)[<span style="color:rgb(104,151,187)">0</span>]<br>    <span style="color:rgb(114,115,122)">directory </span>= os.path.dirname(path_layer)<br>    <span style="color:rgb(114,115,122)">extensions </span>= [<span style="color:rgb(106,135,89)">"shp"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"shx"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"dbf"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"prj"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"sbn"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"sbx"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"fbn"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"fbx"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"ain"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"aih"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"ixs"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"mxs"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"atx"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"xml"</span><span style="color:rgb(204,120,50)">,<br></span><span style="color:rgb(204,120,50)">                  </span><span style="color:rgb(106,135,89)">"cpg"</span><span style="color:rgb(204,120,50)">, </span><span style="color:rgb(106,135,89)">"qix"</span>]<br>    output_path=path_layer<br>    <span style="color:rgb(204,120,50)">if </span>os.path.exists(path_layer):<br>        <span style="color:rgb(204,120,50)">if </span><span style="color:rgb(136,136,198)">len</span>(QgsProject.instance().mapLayersByName(diff_layer)) > <span style="color:rgb(104,151,187)">0</span>:<br>            lyr = QgsProject.instance().mapLayersByName(diff_layer)[<span style="color:rgb(104,151,187)">0</span>]<br>            <span style="color:rgb(136,136,198)">print</span>(<span style="color:rgb(106,135,89)">'renaming layer1: '</span><span style="color:rgb(204,120,50)">, </span><a href="http://lyr.id/" target="_blank">lyr.id</a>())<br>            QgsProject.instance().removeMapLayer(<a href="http://lyr.id/" target="_blank">lyr.id</a>())<br><br>        QgsVectorFileWriter.deleteShapeFile(path_layer)</pre></div><div>Often some handles still remains and I'm not able to delete .dbf and .shp files</div><div>How can I solve this error?</div><div><br></div><div>Thanks</div><div>Pierluigi</div><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Ing. Pierluigi De Rosa (PhD in Earth Science)</div><div dir="ltr">Contract Professor of Geographic Information System at University of Perugia<br>cel: 349755826<span><span dir="ltr">8</span></span> / fax: 075 7823038<br>skype: pierluigi.derosa </div></div></div></div></div></div></div>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>