Is there a quick way to clear all attributes from a layer but leave the polygons in place?

You can enter the following code in the Python Console to clear ALL attributes to NULL for a shapefile loaded into QGIS. Select the layer from the layers panel (Table of Contents) and run the code:

layer = qgis.utils.iface.activeLayer()     
layer.startEditing()   
for field in layer.dataProvider().attributeIndexes():   
    for feature in layer.getFeatures(): 
        layer.changeAttributeValue(feature.id(), field, NULL)    

layer.commitChanges()        

This was tested on QGIS 2.8.2.


UPDATE:

In response to the comment by @Vince, the following code can be directly copied/pasted into the Python console and will change the values of attributes depending on the type of field (i.e. 0 for integer fields; NULL for string fields; and an epoch of 1900-01-01 for a Date field):

layer = qgis.utils.iface.activeLayer()     
layer.startEditing()  
for field in layer.pendingFields():
        if field.typeName() == 'Integer':
                name_int = field.name()
                for feature in layer.getFeatures():
                        feature[name_int] = '0'
                        layer.updateFeature(feature)
        if field.typeName() == 'String':
                name_str = field.name()
                for feature in layer.getFeatures():
                        feature[name_str] = NULL
                        layer.updateFeature(feature)
        if field.typeName() == 'Date':
                name_dat = field.name()
                for feature in layer.getFeatures():
                        feature[name_dat] = '1900-01-01'
                        layer.updateFeature(feature)
layer.commitChanges()

Can't you just copy-and-paste your data into Layer X (a layer/shapefile/feature class/whatever) that has no attributes (besides OID and Geometry) and then copy-and-paste the now "empty" geometry back into your original layer?


You can simply save it as a new file and tick the "Skip Attribute Creation" box in the "Save as..." dialog. It does exactly what it says.

There will be one numeric counter column named FID, apparently the Shapefile format requires at least one attribute? If you can, use something better, spatialite or geopackage.