In my script I make a copy of input layer and perform some actions on it. I can't figure out how to stop QGIS from returning the output layer on error. I've tried returning nothing, but if parameter was defined it gets returned no matter what. instance.removeParameter() also has no effect. Here is a minimal example of my code:

from qgis.processing import alg
from qgis.core import (QgsField,
from PyQt5.QtCore import QVariant

@alg(name='somename', label='somelabel',
     group='somegroup', group_label='somegroup')
@alg.input(type=alg.SOURCE, name='INPUT', label='In')
@alg.input(type=alg.SINK, name='OUTPUT',
def skryptTN(instance, parameters, context, feedback, inputs):
    source = instance.parameterAsSource(parameters, 'INPUT', context)

    flds = source.fields()

    (sink, dest_id) = instance.parameterAsSink(parameters, 'OUTPUT', context,
                                               flds, source.wkbType(), source.sourceCrs())

    for in_feat in source.getFeatures():

        out_feat = QgsFeature()


        for i in source.fields():
            name = i.name()
            out_feat[name] = in_feat[name]


        if in_feat['field_1']:
            out_feat['field_1'] = 'something'
            feedback.reportError(f'Error because of reasons!')
            return # this is not stoping output feature sink from being returned

        sink.addFeature(out_feat, QgsFeatureSink.FastInsert)

    return {'OUTPUT': dest_id}

How to stop script from creating and returning output layer ?

2 Answers 2


Try to use

raise Exception('Error because of reasons!')

rather than or in addition to


to prevent output layer from being created and returned.

  • 1
    This shows a rather cryptic traceback to the user, is there a cleaner way? Mar 12, 2023 at 14:29
  • yes -> QgsProcessingException
    – pathmapper
    Nov 6, 2023 at 16:07

You could use https://qgis.org/pyqgis/3.28/core/QgsProcessingException.html :

from qgis.core import QgsProcessingException
raise QgsProcessingException("Your message here")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.