68

I try to delete an empty folder, but I can't because the windows dialog says it is used at the moment.

How can a empty folder be used?? How can I solve this, without having to restart the computer or log off?

Braiam
  • 4,709
  • 3
  • 26
  • 57
Black
  • 7,349
  • 17
  • 40
  • 61
  • 6
    Your going to need to restart your system to delete the folder. You will have to determine which process is preventing the deletion of the folder. – Ramhound Jun 21 '18 at 11:02
  • 30
    It's always worth a quick check to see if you have a shell (Cmd, Bash, etc) running with the folder as its current directory. This is the usual reason that an empty folder is flagged as in use. – AFH Jun 21 '18 at 12:04
  • 17
    The only real solution is to reboot. Ideally, into Linux as I did a few years ago. This solved the problem permanently. – maaartinus Jun 21 '18 at 21:01
  • 3
    IObit Unlocker: "Never Worry about 'Cannot Delete Files' on Your PC" ... or the equivalent. That answer sounds too much like work. – Mazura Jun 22 '18 at 00:53
  • 7
    Glad to know this is still an issue [8 years later](https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows) – txtechhelp Jun 22 '18 at 06:58
  • 1
    This is occasionally symptomatic of a virus. – Willtech Jun 23 '18 at 10:50
  • Use `resmon.exe` to find out which process is using the folder, as detailed in this answer: [How do I delete a folder that's in use?](https://superuser.com/a/774290/53547) – mivk Aug 18 '18 at 11:25
  • 2
    I've had temp file generated by Emacs (the one that start with #) was not able to delete it any way (Process Explorer say it was locked by system but I was not able to unlock it), when I rebooted it disappear, it probably was some glitch in the system, the file was deleted but appear on disk. That's why I prefer Linux (use it on personal machine), if you have a choice use Linux. Even Windows 10 with Ubuntu in WSL is not as good as real GNU/Linux. – jcubic Feb 15 '19 at 15:46

10 Answers10

100

You will need to use Microsoft's Process Explorer (just an exe): https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer

It will let you know which app is handling that folder and you will also be able to kill that handle.

Find -> Find handle -> search for folder's name (will take a while..)

Visual tutorial: enter image description here

Black
  • 7,349
  • 17
  • 40
  • 61
datester
  • 901
  • 1
  • 7
  • 6
  • 5
    Or use the handle.exe utility (also on the sysinternals site) for a command line solution. – ErikE Jun 21 '18 at 16:22
  • 10
    This absolutely works. Only drawback is: You find software running (as services) on your machine that are holding those handles for seemingly no good reason and it just leaves you frustrated that relatively useless stuff is so invasive and written so badly... (though once you think about it, it makes sense …) – davidbak Jun 21 '18 at 16:40
  • 3
    Alternatively, before going through all that, you might want to try rebooting and try again. If it is still locked, then something that gets launched at boot or log on time is grabbing it when it launches. – jpmc26 Jun 21 '18 at 17:35
  • 18
    [Do note that forcing handles closed can cause data corruption in certain circumstances](https://technet.microsoft.com/en-us/library/2009.04.windowsconfidential.aspx). Explorer should handle it fine, but many programs may not – Justine Krejcha Jun 21 '18 at 18:16
  • 5
    I'd recommend killing the process that owns the handle instead of closing the handle.. You should think twice either way... – Alec Istomin Jun 22 '18 at 17:26
  • 2
    @davidbak that's the hell and reality of Windows innit :D – cat Jun 23 '18 at 04:06
  • This did not work for me. When searching the folder, no processes are found. – Rich M Nov 13 '19 at 11:19
18

I found another way. You can also solve it by restarting explorer.exe.

However, this does not work if it is locked by another process other than explorer.exe.

You can do this from Task Manager. Open task manager from taskbar or with Ctrl+Shft+Esc, then if you don't have a windows explorer tab open, open one.

In Task Manager, when you select other apps, the action button says "Quit", but if you select file explorer, the button says "Restart".

Rohit Gupta
  • 2,721
  • 18
  • 27
  • 35
Black
  • 7,349
  • 17
  • 40
  • 61
13

You can follow these steps to find which program is locking folder:

  1. Run following command on command prompt wmic process > processes.txt. This will create an output file processes.txt with details of all process running.
  2. Open file in processes.txt in a text editor.
  3. Search for locked folder name. You will get Name and PID or process accessing folder.
  4. Kill process using TaskManager or using command TaskKill /PID <pid>. Where <pid> is PID found at step #3.
Sandeep
  • 1,160
  • 6
  • 14
  • I wish this worked. It didn't. CD C:\ - md temp - cd temp - md utemp - cd utemp - notepad - cd .. - rd utemp (failed due to notepad) - wmic process > processes.txt - the result ing processes.txt did not contain "utemp", – TOOGAM Jun 24 '18 at 09:18
  • @TOOGAM Thanks for pointing that out. I am surprised to see that it actually happened with Notepad. Tested Notepad++ too and was able to delete folder without any issue. +1 for accepted answer. – Sandeep Jun 24 '18 at 10:37
13

Sysinternals suite has already been mentioned, but ProcessExplorer is actually the wrong tool. movefile is what you want. Killing the process that has the directory open may work, but it is not a good (or safe) approach.

With movefile you can, well... move files (directories are files!) on the next reboot. Moving a file to no destination, i.e. "" schedules deletion on next reboot. Note that it needs to be run from an elevated prompt (otherwise you get Error 5 trying to schedule boot-time deletes).

Damon
  • 4,512
  • 3
  • 22
  • 28
8

You can forcefully terminate the process responsible for "locking" folder given the process ID is known, and any good process killer can do that without having to restart the system.

Alternatively with a GUI you could use Iobit unlocker to unlock the folder, and thus delete the folder afterward. Drag the target folder on the iobit interface then, on the scrollbar beside select unlock and delete.

Sample screenshot

Vadim Kotov
  • 113
  • 5
xavier_fakerat
  • 2,502
  • 2
  • 13
  • 24
  • 1
    Disclaimer!! no affiliation to the software, but just happen to be an easy GUI based way to do it – xavier_fakerat Jun 21 '18 at 12:14
  • 3
    Using this software might cause the process that has the lock on the file/folder to crash or become unstable. I would suggest using datester's answer – Cfinley Jun 21 '18 at 17:13
  • 1
    Windows may reuse the handle for another file, which can lead to really nasty stuff: the application that locked the file will now unknowingly work on another file, which can potentially have disastrous effects. – gronostaj Jun 22 '18 at 08:42
  • 1
    Ok fair enough to those who insist this method is bad, but trust me it still gets the job done for me without even breaking a sweat. Besides there are just too many ways to "kill this cat" – xavier_fakerat Jun 22 '18 at 10:22
  • 1
    @xavier_fakerat You either didn't understand the problem that gronostaj lined out or you have a very cavalier attitude towards your files. – Voo Jun 23 '18 at 09:36
  • @Voo not atll, I am not "careless" with files, but it's now too late to take down my answer , I would have taken it down it my if that sounded like my impression :( – xavier_fakerat Jun 24 '18 at 06:57
  • @gronostaj this does not sound plausible to me. Do you have any evidence, that this is true? Of course handles are reused by operating system, but not before they are closed. Could you please clarify what possible scenario could cause software "unknowingly work on another file" and how does it relate to the program in question? – Andrew Savinykh Jun 24 '18 at 23:59
  • @AndrewSavinykh Closing file handles is exactly what "unlockers" do. https://technet.microsoft.com/en-us/library/2009.04.windowsconfidential.aspx – gronostaj Jun 25 '18 at 06:34
  • @gronostaj thank you for the link, turns out you are right ;) Thank you for explaining that to me. I wonder if now, 10 years later there is a mechanism that prevents a process using a handle it did not legitimately obtain. – Andrew Savinykh Jun 25 '18 at 07:00
  • @AndrewSavinykh: kernel handles (such as file handles) are already per-process, so there's no risk a process will mess with a file opened by another one by using a stale handle. What can happen is that the handle gets reused *in the same process*, but Windows has no way to recognize the "real" new handle from the old handle that happened to have the same value. – Matteo Italia Jun 25 '18 at 11:34
  • How does this work if the folder is "empty", even with Show hidden files turned on? – Rich M Nov 13 '19 at 11:21
  • this works for me! – wang.chance Jul 29 '20 at 16:29
3

I also had this problem before, and I solved it by going to the Git Bash, and using rm -rf my-directory.

I have no idea why it worked tho, maybe Git bash has more right than the windows command.

If you don't know Git, you can download it by typping 'Git' on Google, install it and then just right click on your explorer, where the directory is, and click on 'Git bash here'.

HypnoZ
  • 31
  • 1
2

I've actually noticed in the past that even with hidden files on and all the bells and whistles saying to show all files that certain programs would still have files that if saved with them were completely invisible in explorer. That's likely the case here. What I would do is go into whatever programs you were using this folder with and go to open file (if applicable) and see what is actually in the file in that view. There might be a lot of junk windows seems intent on hiding from you.

user64742
  • 247
  • 1
  • 14
2

Restarting windows explorer did the trick for me. Just go to task manager and restart explorer.

0

I've had this problem before, years ago when I used a website's automatic convert/download to change a page's html into a txt file download. The way Windows deals with the naming of files made it so that the quotations in the title changed the filetype to whatever was after the quotes, and made it undeletable. The solution I found was also described as being able to delete folders, so it should help you too.

First, open command prompt with admin privileges. Say that the folder I want to delete is in C:\Users\XXXXXX\Downloads

Make sure to include spaces wherever I make them, or else it won't work

Type in the command:

cd /d C:\Users\XXXXXX\Downloads

Next type:

dir /x

That will show you a list of all items within Downloads, with the date/time created followed by a ( <DIR> ) if a folder / a ( 0 ) if a file, the name in a shortened format, and then the full name.

Find the shortened name of the folder you want to delete and then type it exactly as shown in the list - capitalized, numbers, tildes, whatever (without the labeling 0 if a file) - minus the brackets I include here in the example:

rmdir /q /s [SHORTNAMEHERE]

And that's it! If you want to delete a file, just change rmdir to erase, and you're good! Just make sure that the folder you want to delete is actually empty and has nothing important in it first.

0

I launched a powershell process from a folder via win cmd.
After powershell finished, I cd .. in win CMD but rd /q /s gave error: open in another process
I get same error when manually delete folder in explorer window, even if it was empty.

wmic process revealed that powershell.exe could be locking the folder, even though powershell had finished the task & powershell window had closed. This was confirmed by tasklist

I then taskkill /im powershell.exe, after which the folder could be deleted.

Zimba
  • 1,051
  • 11
  • 15