I am creating a standalone PyQGIS application and is met with a problem.

When a shapefile is shown on my canvas, I am unable to delete the corresponding .shp and .dbf files in my directory.

This is the error I get:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Data\\tmpy187h2zs\\0-Boundary\\0-Boundary.dbf'

This does not happen when I try to close raster files.

I am using PyQt for the GUI. And there's a workaround to this problem by having 2 buttons. Remove from canvas and Delete.

After pressing the Remove from canvas button, I am able to delete the layer. However, when I combine both of these buttons together, an error occurs.

Here is the function for remove layer and delete:

Remove layer:

def removeAllLayers(Main):
    layers = Main.layers
    registry = QgsProject.instance()
    if len(layers) != 0:
        layers = []


def deleteFiles(Main):
    for files in os.listdir(Main.tempPath + '/' + button):
        os.remove(Main.tempPath + '/' + button + "/" + files)
    print("Files Deleted")

When combined:

def removeAndDeleteFile(Main):

and these functions are just embedded into QPushButton:

pushButtonRemoveFromCanvas.clicked.connect(lambda: removeAllLayers(self))
pushButtonDeleteFiles.clicked.connect(lambda: deleteFiles(self))
pushButtonRemoveAndDelete.clicked.connect(lambda: removeAndDeleteFile(self))

This works:

  1. Click pushButtonRemoveFromCanvas
  2. Click pushButtonDeletFiles

This does not work:

  1. Click pushButtonRemoveAndDelete Returns PermissionError as mentioned above

The main problem stems from that the .shp and .dbf files are not deleted. I have browsed other threads such as can't erase dbf and shp pyQGIS and Deleting raster file after doing some processing in QGIS? but they do not directly answer the question and is a workaround specific to their question.

I have been on this problem for weeks and is unable to solve it, wonder if anyone can help me.

  • 1
    Please confirm that you know that when a shapefile is opened in read-write mode the files get locks. When you close that shapefile the locks are removed and you can delete the files. Now for some reason locks are not removed when your combined button tries to start delete. – user30184 Oct 16 '20 at 8:29
  • @user30184 I have seen this thread gis.stackexchange.com/questions/77218/… but they don't seem to have a solution for the problem either. – Sean Ang Oct 16 '20 at 8:59
  • Unix will let you delete opened files, but Windows won't. You haven't made any reference to the third mandatory component in a shapefile, the .shx file. – Vince Oct 16 '20 at 11:50
  • as in the link you provided, try calling the garbage collector (gc.collect()) between the two calls. – JGH Oct 16 '20 at 12:21
  • @JGH adding gc.collect() doesn't work. Still creates permissionError – Sean Ang Oct 16 '20 at 12:27

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.