QGIS Expression – Delete Specific Rows from Attribute Table

attribute-tableexpressionqgisqgis-expression

I have a shapefile with one column. From that column I want to keep only the records (rows) with value 50 and delete the other rows. So far, I am doing this manually, by selecting the rows with the values I want to delete and I remove them. I was wondering if there is a way to write an expression in QGIS to do this automatically. I have see this post but it's not what I am looking for. Assuming that my attribute table's values is from 20 to 100 with step 10 and the column name is DN, I want to delete all rows except the ones that have a value of 50. Below is a screenshot of the attribute table:

attribute

Best Answer

While the layer is selected, open "Select by Expression (Ctrl+F3)" window. Select features using this expression:

DN != 50

Start Editing mode for the layer by clicking yellow pen icon. Click the bin icon to delete the selected features. Finish the Editing mode and save the changes.

enter image description here


Another option would be to use "Extract by Expression" tool. This tool doesn't delete the features, but extract the features which match the expression. In that case, you need to use DN = 50.

enter image description here

Related Question