code: def clean_layer(layer):
      
      output_layer_name = clean_geometry_steps['output_layer_name']
      output_layer_name = output_layer_name % layer.keywords['layer_purpose']
  
      # start editing
      layer.startEditing()
      count = 0
  
      # iterate through all features
      request = QgsFeatureRequest().setSubsetOfAttributes([])
      for feature in layer.getFeatures(request):
          geom = feature.geometry()
          was_valid, geometry_cleaned = geometry_checker(geom)
  
          if was_valid:
              # Do nothing if it was valid
              pass
          elif not was_valid and geometry_cleaned:
              # Update the geometry if it was not valid, and clean now
              layer.changeGeometry(feature.id(), geometry_cleaned, True)
          else:
              # Delete if it was not valid and not able to be cleaned
              count += 1
              layer.deleteFeature(feature.id())
  
      if count:
          LOGGER.critical(
              '%s features have been removed from %s because of invalid '
              'geometries.' % (count, layer.name()))
      else:
          LOGGER.info(
              'No feature has been removed from the layer: %s' % layer.name())
  
      # save changes
      layer.commitChanges()
      layer.keywords['title'] = output_layer_name
  
      check_layer(layer)
      return layer
docs: Clean a vector layer.
  
      :param layer: The vector layer.
      :type layer: QgsVectorLayer
  
      :return: The buffered vector layer.
      :rtype: QgsVectorLayer