Skip to main content
Adaptations for QGIS version 3 and above added.
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. 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.

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     from PyQt4.QtCore import QDate, QDateTime
    
     lyr = iface.activeLayer()
     lstDelete = []
    
     for idx in lyr.dataProvider().attributeIndexes():
         uv = 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.

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     from qgis.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.

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     from PyQt4.QtCore import QDate, QDateTime
    
     lyr = iface.activeLayer()
     lstDelete = []
    
     for idx in lyr.dataProvider().attributeIndexes():
         uv = 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.

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     from PyQt4.QtCore import QDate, QDateTime
    
     lyr = iface.activeLayer()
     lstDelete = []
    
     for idx in lyr.dataProvider().attributeIndexes():
         uv = 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.

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     from PyQt4.QtCore import QDate, QDateTime
    
     lyr = iface.activeLayer()
     lstDelete = []
    
     for idx in lyr.dataProvider().attributeIndexes():
         uv = 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.

Updated the answer since previous version was not running properly.
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     from PyQt4.QtCore import QDate, QDateTime
    
     lyr = iface.activeLayer()
     lstDelete = []
    
     for idx in lyr.dataProvider().attributeIndexes():
         uv = lyr.dataProvider().uniqueValues( idx )
         if notlen( uv ) == 1:
             if uv[0] == NULL or uv[0] == QDateTime() or uv[0] == QDate() or uv[0] == '':
                 lstDelete.append( idx )
    
     if not lyr.dataProvider().deleteAttributescapabilities() lstDelete& )
    QgsVectorDataProvider.DeleteFeatures:
     # Update the  print "This layer structureprovider 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.

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     lyr = iface.activeLayer()
     lstDelete = []
    
     for idx in lyr.dataProvider().attributeIndexes():
         uv = lyr.dataProvider().uniqueValues( idx )
         if not uv:
             lstDelete.append( idx )
    
     lyr.dataProvider().deleteAttributes( lstDelete )
    
     # Update the layer structure
     lyr.updateFields()
    

This should delete all your empty fields for the active layer.

Follow this workflow:

  1. Open QGIS, load your Shapefile layer, and select it (i.e., make it active) in the ToC.

  2. Open the QGIS Python console.

  3. Copy the following code snippet and paste it in the QGIS Python console:

     from PyQt4.QtCore import QDate, QDateTime
    
     lyr = iface.activeLayer()
     lstDelete = []
    
     for idx in lyr.dataProvider().attributeIndexes():
         uv = 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.

Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180
Loading