Using PyQGIS for a standalone plugin, I try to remove a file I created before as an output file for QGIS processing:

    layers = [layer for layer in QgsProject.instance().mapLayers().values()]
    inputIndex = self.dlg.layerlist.currentIndex()
    input = layers[inputIndex]

    output = self.dlg.directory.text()

    # run algorithms
    params_snap = {
        'input' : input,
        'type' : 1,
        'tool' : 1,
        'threshold' : 0.000095,
        'output': output,
        'error' : 'TEMPORARY_OUTPUT',
    processing.run('grass7:v.clean', params_snap)
    context = QgsProcessingContext()
    input = QgsProcessingUtils.mapLayerFromString(output, context, True)
    if os.path.exists(output):

But I get a [WinError 32] The process cannot access the file because it is being used by another process. As you see in the code I tried adding QgsProject.instance().removeMapLayer(output) to remove the file from QGIS first, but it doesn't work. Any advice?

What I try to do by deleting the output file from the algorithm is to run a second (and third...) algorithm after the first one, using the same file as the output.

  • Perhaps try using del output? – Joseph May 31 at 9:54
  • The goal I want to achieve is, that I want to run a consecutive proessing algorithm with the same output file. If I use del output I get the following error in the second algorithm that uses output: A file system object called 'C:\Users\...' already exists. I will edit my question to add the overall goal I try to achieve using remove. – gHupf Jun 3 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.