I wrote a progressing algorithm using Python. The tool uses multiple other processing tools in the toolbox and creates temporary outputs. Those outputs seem somehow saved while I was running the script while developing. Now, when I call my tool from the toolbox and want to select layer as an input, they appear in the selection even though they are created a while ago and they are not even loaded with QGIS Project that I am working.

enter image description here

This is how the input parameter in my script

        self.tr('Pfad zur geologischen Karte'),

Any idea how I can get rid of them?

I think this part of the script is producing those temporary layers

res2 = processing.run("native:clip", {
'INPUT': vlyr_ger,
'OVERLAY': vlyr_ezgStats,
vlyr_gerClp = res2['OUTPUT']
  • 1
    Are these layers still in the QGIS document's table of contents? If so then you will have to remove them from there and then they should not be selectable options anymore. – wfgeo Jun 8 '20 at 16:11
  • 1
    No, they are not in the TOC. That's why it surprises me to see them appear as selectable options. – Nil Jun 8 '20 at 16:14
  • 1
    Probably your script or other algorithms are adding layers to QGIS layer registry (and not to the Layers panel). We could fix it in the script if you're willing to share it (or simply, if you call QgsProject.instance().addMapLayer(my_layer, False), just remove that False) or if you're fine, we can also remove them from the project, after the script is executed, with a couple of lines. Which solution fits best for your use case? – Germán Carrillo Jun 8 '20 at 16:37
  • 1
    What is the version of QGIS (full version number like 3.10.1 or 3.4.13-3)? – Kadir Şahbaz Jun 8 '20 at 22:14
  • A weird problem. It is hard to tell what the problem is without seeing other parts of the script. – Kadir Şahbaz Jun 9 '20 at 0:02

If you change .QGZ file's extension as .ZIP and extract .QGS file in it and open that .QGS file in a text editor, you will see XML-like content. You will see names of layers which exist in QGIS layer tree, and their ids. I guess, in the following lines, you will see extra ids which don't exist in QGIS layer tree but exist somehow in .QGS file. When you want to choose an input layer in a processing tool, you see those layer. (I guess, not sure 100%).

After I deleted some layers in .QGS file and opened it in QGIS and run a processing tool, I encountered the same problem. Although layers returned by other tools is not added to the layer tree, somehow their ids must be remaining in the project file.

For a temporary solution to get rid of those names, use the following script. It removes layers which don't exist in layer tree.

p = QgsProject.instance()

tree_layers = [l.layer() for l in p.layerTreeRoot().children()]
map_layers = p.mapLayers().values()

for l in map_layers:
    if not l in tree_layers:
  • 1
    del b ( del clipped, del vlyr_gerClp in my case) allows me only to delete the ones that my script newly created. However, my qgis project still shows the ones from previous sessions. I have deleted my temporary files in the temp folder but still cannot get rid of :/ Maybe I should just create a new QGIS Project -_- – Nil Jun 9 '20 at 7:14
  • @jugoslaviaa I guess this solution doesn't solve the other issue. I mean when you run your processing script which contains other tools, same layer names probably will be appear again. – Kadir Şahbaz Jun 9 '20 at 8:31
  • that's correct. Unfortunately, it did not solve the problem either. – Nil Jun 15 '20 at 14:37
  • If you share a reproducible example of your script, we can help you find a solution. Which and how tools do you use in the script? We don't know the structure of your script. – Kadir Şahbaz Jun 15 '20 at 15:11
  • The above script doesn't take into account if your layer tree contains layer groups and will produce a traceback if your tree contains layer groups. But it does do what I needed, iill see if i can write a check on the layer group object type and ill post here. – nr_aus Sep 29 '20 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.