Follow this workflow:
Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.
Open the QGIS Python console.
Copy the following code snippet and paste it in the QGIS Python console:
from PyQt4qgis.PyQt.QtCore import QDate, QDateTime lyr = iface.activeLayer() lstDelete = [] for idx in lyr.dataProvider().attributeIndexes(): uv = list(lyr.dataProvider().uniqueValues( idx )) if len( uv ) == 1: if uv[0] == NULL or uv[0] == QDateTime() or uv[0] == QDate() or uv[0] == '': lstDelete.append( idx ) if not lyr.dataProvider().capabilities() & QgsVectorDataProvider.DeleteFeatures: print "This layer provider does not support deleting attributes." else: lyr.dataProvider().deleteAttributes( lstDelete ) lyr.updateFields() # Update the layer structure
This should delete all your empty fields for the active layer.
Update: In QGIS version 3 and above the uniqueValues
method returns a set. Therefore each uv[0]
must be replaced with next(iter(uv))
in order for the code to work.