Search code examples
vbafilems-accessdelete-filefso

Deleting a folder using VBA and it still appears in File Explorer until the next iteration


VBA - Access

I'm using fso.DeleteFolder to delete a folder on a server that an earlier function in the same Access DB created. The fso.DeleteFolder code runs without error and running fso.FolderExists after shows the folder no longer exists (prior to the delete, fso.FolderExists stated that the folder did exist).

However the folder is still listed in File Explorer on the server. Refreshing does no good. When double-clicking the folder on the server, I get an error stating that the folder is not accessible and access denied (prior to the delete, the folder was normal).

The folder may or may not have subfolders and/or files - but I do not think that is important because if I run the create / delete processes over again using a different folder name, the issue repeats - EXCEPT the prior folder is now no longer listed in File Explorer (truly deleted now as God intended), replaced with the new folder that was supposed to be deleted with the same double-click error and so on.

I kill my fso object at the end of the function, I believe the same issue also happens with KILL instead of fso.

Any help/thoughts would be appreciated.


Solution

  • Just encountered that problem!

    For some reason deleting a directory with VBA leaves that lingering "lock" on the deleted file UNTIL it performs another directory type action then it flushes the "lock" If you were to delete three directories in one script you would likely find that the only "failed" delete action would be on the last directory deleted.

    Performing a "directory read" type action within the script after the RmDir action works for me. Try something like this...

    If Dir("yourpath") = "" Then MkDir "yourpath"