<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'>
<p>Thank you, Nyall. That works.</p>
<p>Simon</p>
<p><br /></p>
<p id="reply-intro">Am 05.06.2025 01:23, schrieb Nyall Dawson:</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">
<div class="pre" style="margin: 0; padding: 0; font-family: monospace">On Mon, 2 Jun 2025 at 19:16, Simon Gröchenig via QGIS-Developer<br /><<a href="mailto:qgis-developer@lists.osgeo.org">qgis-developer@lists.osgeo.org</a>> wrote:
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0"><br />Hi,<br /><br />how can I remove a layer variable with Python?<br /><br />The following returns true, but does not work.<br /><br />QgsExpressionContextUtils.layerScope(layer).removeVariable("variable_name")</blockquote>
<br />That's cos it's just removing the variable from a copy of the layer's<br />expression context scope, not from the layer itself.<br /><br />It's rather gross, but try<br /><a href="https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.setLayerVariables" target="_blank" rel="noopener noreferrer">https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.setLayerVariables</a><br />-- you'll need to first retrieve the existing variables via<br /><a href="https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.layerScope" target="_blank" rel="noopener noreferrer">https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.layerScope</a><br />and then build a dictionary of the variables you want to keep by<br />iterating over <a href="https://qgis.org/pyqgis/3.40/core/QgsExpressionContextScope.html#qgis.core.QgsExpressionContextScope.variableNames" target="_blank" rel="noopener noreferrer">https://qgis.org/pyqgis/3.40/core/QgsExpressionContextScope.html#qgis.core.QgsExpressionContextScope.variableNames</a><br /><br />Hope that helps!<br />Nyall<br /><br /><br />
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0"><br />Best regards<br />Simon<br /><br />_____________________<br />Simon Gröchenig<br />GIS Support und Training<br />Treffling 73 - 9871 Seeboden - Österreich<br />Tel.: +43 699 11 22 31 22<br />Web: <a href="https://www.sgroe.at" target="_blank" rel="noopener noreferrer">https://www.sgroe.at</a><br />E-Mail: <a href="mailto:gis@sgroe.at">gis@sgroe.at</a><br />_______________________________________________<br />QGIS-Developer mailing list<br /><a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a><br />List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank" rel="noopener noreferrer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br />Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank" rel="noopener noreferrer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a></blockquote>
</div>
</blockquote>
<p><br /></p>
<div id="signature">-- <br />
<div style="color: #000000; font-family: sans-serif; background-color: #ffffff;"><span style="font-size: small;">_____________________</span></div>
<div style="color: #000000; font-family: sans-serif; background-color: #ffffff;"><span style="font-size: small;">Simon Gröchenig</span></div>
<div style="color: #000000; font-family: sans-serif; background-color: #ffffff;"><span style="color: #7e8c8d; font-size: small;">GIS Support und Training</span></div>
<div style="color: #000000; font-family: sans-serif; background-color: #ffffff;"><span style="color: #7e8c8d; font-size: small;">Treffling 73 - 9871 Seeboden - Österreich</span></div>
<div style="color: #000000; font-family: sans-serif; background-color: #ffffff;"><span style="color: #7e8c8d; font-size: small;">Tel.: +43 699 11 22 31 22</span></div>
<div style="color: #000000; font-family: sans-serif; background-color: #ffffff;"><span style="color: #7e8c8d; font-size: small;">Web: <a style="color: #7e8c8d;" href="https://www.sgroe.at">https://www.sgroe.at</a> </span></div>
<div style="color: #000000; font-family: sans-serif; background-color: #ffffff;"><span style="color: #7e8c8d; font-size: small;">E-Mail: <a href="mailto:gis@sgroe.at"><span style="color: #7e8c8d;">gis@sgroe.at</span></a></span><span style="color: #7e8c8d; font-size: small;"> </span></div>
</div>
</body></html>