On Mon, 2 Jun 2025 at 19:16, Simon Gröchenig via QGIS-Developer <[email protected]> wrote: > > Hi, > > how can I remove a layer variable with Python? > > The following returns true, but does not work. > > QgsExpressionContextUtils.layerScope(layer).removeVariable("variable_name")
That's cos it's just removing the variable from a copy of the layer's expression context scope, not from the layer itself. It's rather gross, but try https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.setLayerVariables -- you'll need to first retrieve the existing variables via https://qgis.org/pyqgis/3.40/core/QgsExpressionContextUtils.html#qgis.core.QgsExpressionContextUtils.layerScope and then build a dictionary of the variables you want to keep by iterating over https://qgis.org/pyqgis/3.40/core/QgsExpressionContextScope.html#qgis.core.QgsExpressionContextScope.variableNames Hope that helps! Nyall > > Best regards > Simon > > _____________________ > Simon Gröchenig > GIS Support und Training > Treffling 73 - 9871 Seeboden - Österreich > Tel.: +43 699 11 22 31 22 > Web: https://www.sgroe.at > E-Mail: [email protected] > _______________________________________________ > QGIS-Developer mailing list > [email protected] > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer _______________________________________________ QGIS-Developer mailing list [email protected] List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
