How to remove a directory in Windows even if it is in use?

I have a database service running in a known directory C:\meuBanco\exe\ and have now developed an updater of this service, which performs the following steps:

  1. for service
  2. exclude folder exe: rd /s /q %DIRSERV%\exe\
  3. unzips the zip with the new service in the folder exe

The problem is in Step 2, where eventually it is not possible to delete the folder as it is in use. But when the update runs, it can delete the folder because you shouldn't have any process blocking the exe folder.

So I ask: is there a way to force the removal of a directory on Windows even if it is in use?

Author: Murillo Goulart, 2017-04-11

1 answers

You can test these two options below. as this answer in SOEn:

Be careful when executing these commands! after running, the process is irreversible and can cause damage to an application or operating system if it runs on windows folders.

takeown /r /f [path da pasta]
cacls [path da pasta] /c /G "ADMINNAME":F /T
rmdir /s [path da pasta]

Another alternative for long paths, which can cause errors with the above method:

mkdir \empty
robocopy /mir \empty [path da pasta]

Obs.: the user to execute both commands needs to have elevated administrator privileges.

 3
Author: Comunidade, 2017-05-23 12:37:23