<div dir="ltr">Oh you're right, my mistake. Here is a slightly modified version of the function that relies on the QgsLayerTree instead of the QgsCanvas. This one won't remove layers that are only unchecked in the layer tree.<div><br></div><div>def remove_odd_layers():<br>    project = QgsProject.instance()<br>    project_layers = project.mapLayers().values()<br>    tree_layers = [l.layer() for l in project.layerTreeRoot().findLayers()]<br>    for layer in project_layers:<br>        if layer not in tree_layers:<br>            project.removeMapLayer(layer)<br></div><div><br></div><div><br></div><div>Regards,</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le jeu. 27 août 2020 à 22:03, Jens Beutmann <<a href="mailto:beutmann@arcor.de">beutmann@arcor.de</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>
    <div>That seems to be working. Thanx a lot,
      Yoann!!!</div>
    <div> Before running the function, you have
      to switch on in the layer dialog all the layers you want to keep,
      because otherwise the function kills all layers that are not
      visible. But after doing that, I got the result I wanted.</div>
    <div>That helped me a lot!<br>
    </div>
    <div><br>
    </div>
    <div><br>
    </div>
    <div>Am 27.08.2020 um 11:44 schrieb Yoann
      QUENACH DE QUIVILLIC:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">I had the exact same problem some time ago. The
        layers are present in the QgsProject, but not added to the
        QgsCanvas, so they don't appear in the Layer Panel but the entry
        is there in the Layer Styling Panel.
        <div>You can use PyQGIS to solve it
          <div>Here is a simple function that iters over the layers in
            the project, and removes the layers not present in the
            canvas:</div>
          <div>  </div>
          <div>def remove_odd_layers():<br>
                for layer in QgsProject.instance().mapLayers().values():<br>
                    if layer not in iface.mapCanvas().layers():<br>
                        QgsProject.instance().removeMapLayer(layer)<br>
          </div>
          <div><br>
          </div>
          <div>
            remove_odd_layers
            ()</div>
        </div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">Le dim. 23 août 2020 à 13:03,
          Jens Beutmann <<a href="mailto:beutmann@arcor.de" target="_blank">beutmann@arcor.de</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">I
          got a problem with layers that are displayed in the layer
          design <br>
          dialog ("Layergestaltung" in German) but not in the layer
          window itself. <br>
          I would like to delete these layers but this is not possible.
          I opened <br>
          the project in QGIS 2.18 and in 3.10 and it happens in both
          versions. <br>
          Can anybody help?<br>
          <br>
          <br>
          -- <br>
          Diese E-Mail wurde von Avast Antivirus-Software auf Viren
          geprüft.<br>
          <a href="https://www.avast.com/antivirus" rel="noreferrer" target="_blank">https://www.avast.com/antivirus</a><br>
          <br>
          _______________________________________________<br>
          Qgis-user mailing list<br>
          <a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
          List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
          Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a></blockquote>
      </div>
      <br clear="all">
      <div><br>
      </div>
      -- <br>
      <div dir="ltr">
        <div dir="ltr">
          <div>
            <div dir="ltr"><span><br>
                <div dir="ltr" style="margin-left:0pt">
                  <table style="border:none;border-collapse:collapse">
                    <colgroup><col width="158"><col width="529"></colgroup><tbody>
                      <tr style="height:124pt">
                        <td style="vertical-align:top;padding:5pt">
                          <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="https://www.itlink.fr/" target="_blank"><span style="font-size:11pt;font-family:"Open Sans";color:rgb(17,85,204);background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><img src="https://lh3.googleusercontent.com/WYaF3wmob-MGxieg6sbXGkwhWcBc-IEr5CV1W00gYx2_NKokveRsOhIspH1N1IMW6iUb3ha6XUgBNo0mxwlkIIABuzI2yqz7rwtSye9JK400YzNoemBV4P4CYmwr3szDtYzOhhX2" style="border: none;" width="144" height="144"></span></a></p>
                        </td>
                        <td style="vertical-align:top;padding:5pt">
                          <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14pt;font-family:Verdana;color:rgb(0,51,126);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap">
</span></p>
                          <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14pt;font-family:Verdana;color:rgb(0,51,126);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap">Yoann Quenach de Quivillic</span></p>
                          <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Verdana;color:rgb(35,149,244);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap">Ingénieur d'étude</span></p>
                          <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Verdana;color:rgb(35,149,244);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap">
</span></p>
                          <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="https://www.itlink.fr/" target="_blank"><span style="font-size:10pt;font-family:Verdana;color:rgb(17,85,204);background-color:transparent;vertical-align:baseline;white-space:pre-wrap">www.itlink.fr</span></a></p>
                          <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><br>
                          </p>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </span></div>
          </div>
        </div>
      </div>
    </blockquote>
    <p><br>
    </p>
  <div id="gmail-m_6622802105708209006DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2">
<br><br>
<hr style="border:none;color:rgb(144,144,144);background-color:rgb(176,176,176);height:1px;width:99%">
<table style="border-collapse:collapse;border:none">
        <tbody><tr>
                <td style="border:none;padding:0px 15px 0px 8px">
                        <a href="https://www.avast.com/antivirus" target="_blank">
                                <img border="0" src="http://static.avast.com/emails/avast-mail-stamp.png" alt="Avast logo">
                        </a>
                </td>
                <td>
                        <p style="color:rgb(61,77,90);font-family:Calibri,Verdana,Arial,Helvetica;font-size:12pt">
                                Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
                                <br><a href="https://www.avast.com/antivirus" target="_blank">www.avast.com</a>
                        </p>
                </td>
        </tr>
</tbody></table>
<br>
<a href="#m_6622802105708209006_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"> </a></div></div>

</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><span><br><div dir="ltr" style="margin-left:0pt"><table style="border:none;border-collapse:collapse"><colgroup><col width="158"><col width="529"></colgroup><tbody><tr style="height:124pt"><td style="vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="https://www.itlink.fr/" target="_blank"><span style="font-size:11pt;font-family:"Open Sans";color:rgb(17,85,204);background-color:transparent;vertical-align:baseline;white-space:pre-wrap"><img src="https://lh3.googleusercontent.com/WYaF3wmob-MGxieg6sbXGkwhWcBc-IEr5CV1W00gYx2_NKokveRsOhIspH1N1IMW6iUb3ha6XUgBNo0mxwlkIIABuzI2yqz7rwtSye9JK400YzNoemBV4P4CYmwr3szDtYzOhhX2" width="144" height="144" style="border: none;"></span></a></p></td><td style="vertical-align:top;padding:5pt"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14pt;font-family:Verdana;color:rgb(0,51,126);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap"><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14pt;font-family:Verdana;color:rgb(0,51,126);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap">Yoann Quenach de Quivillic</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Verdana;color:rgb(35,149,244);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap">Ingénieur d'étude</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Verdana;color:rgb(35,149,244);background-color:transparent;font-weight:700;vertical-align:baseline;white-space:pre-wrap"><br></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><a href="https://www.itlink.fr/" target="_blank"><span style="font-size:10pt;font-family:Verdana;color:rgb(17,85,204);background-color:transparent;vertical-align:baseline;white-space:pre-wrap">www.itlink.fr</span></a></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><br></p></td></tr></tbody></table></div></span></div></div></div></div>