There are most likely several approaches to tackle your issue, but I would probably lean on @Matt's suggestion.
There is a special method for deleting selected features called deleteSelectedFeaturedeleteSelectedFeatures()
.
Keep in mind that the output of processing will inherit the name of your 'INPUT'
variable.
layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]
selection = processing.run("native:selectbylocation",
{'INPUT': layer1,
'PREDICATE': [0,4,7],
'INTERSECT': layer2,
'METHOD':0})['OUTPUT']
layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]
layer_to_edit.startEditing()
layer_to_edit.deleteSelectedFeatures()
layer_to_edit.commitChanges()
If you though want to retrieve a list with selected features, then try this:
layer1 = QgsProject.instance().mapLayersByName('grid_test2')[0]
layer2 = QgsProject.instance().mapLayersByName('grid_test')[0]
selection = processing.run("native:selectbylocation",
{'INPUT': layer1,
'PREDICATE': [0,4,7],
'INTERSECT': layer2,
'METHOD':0})['OUTPUT']
layer_to_edit = QgsProject.instance().mapLayersByName(selection.name())[0]
selected_ids = [feat.id() for feat in selection.selectedFeatures()]
layer_to_edit.startEditing()
for fid in selected_ids:
layer_to_edit.deleteFeature(fid)
layer_to_edit.commitChanges()
References: