How do I delete a folder I can't delete?
Hi everyone. Leo Notenboom
here for AskLeo.com. A couple of videos
ago, I talked about deleting a folder that I couldn't delete and how it may not
necessarily be worth the time and the effort.
Fine, I get it.
You still want to delete the folder.
Here are some of the reasons it can happen
and some of the things that I try when I find myself in this situation.
So there are a couple of reasons that this can happen.
The most common one, honestly, is that the file is being used by some other program.
That means either identifying which program that is and shutting it down
or usually rebooting the machine and then having deleting that folder be the first
thing you do before you start up any of your other software.
If that still doesn't solve the problem,
firing up a tool like Process Explorer, which can allow you to determine who's
using a file in use and I've got an article on that.
How can I find out who's using a file in use?
We'll walk you through the process of using Process Explorer to see
which process has this file open or this folder open.
But like I said, that's by far the most
common reason that you can't delete a file or a folder number two and three are kind
of mixed together at the low end of the probability scale, but they do happen.
One is that the full path is too long.
Now, this can be a little bit obscure.
Windows, as it turns out,
has a couple of different limitations in the length of a path name.
By path I mean the full C folder slash folder slash folder slash file name.
That whole thing has to be either less than 32,000
characters in some cases, or more commonly, 260 characters.
And that's where we tend to run into some problems from time to time.
The issue is that some tools can work
with the 32,000 limit and some can't somehow are stuck with the 260 limit.
So if you have a file or a folder that its full path from the root of whatever
drive it is, is longer than 260 characters,
some tools just aren't going to be able to deal with it.
And that's unfortunate, but you need to find some ways around that.
We'll talk about a couple of ideas.
The other, and this too happens,
oddly enough, often, even though it should never happen.
And that is simply that the file name has an invalid character in it.
For example, a greater than sign
is used in the command line to indicate something called redirection.
Basically, take the output of this program and redirect it to this file.
There should never be a file name with a greater than character in it.
It's illegal.
And yet sometimes some programs can create
a file that has that character in the file name.
What that means though,
then is that great, the file exists, but none of the tools you might use
to delete the file will accept what you're telling.
It is the file name as the file name, because that should never exist again.
It shouldn't.
Windows somewhere, I guess, has a hole in it that allows it to happen,
but the bottom line is it shouldn't happen, but occasionally it does.
And that can cause this kind of confusion.
So what we're going to do is use
my example folder from a couple of videos ago, because I still have it around.
I haven't deleted it yet.
And I'll look at a couple of the different approaches that I take when
deleting a folder or trying to delete a folder that can't be deleted.
So here we are back in Windows command prompt.
The folder is still here.
I've got a named folder I can't delete, which is very, a very good way to leave
things if you decided that you're not going to waste the energy.
The first thing that I try to do is just delete what I can.
So the delete command, del, is the Windows command prompt.
Delete command slash s means delete subfolders and everything in them.
Q means don't ask me, just do it.
Dot means the current folder.
Now my current folder is e:\t
so what I'm actually trying to do is
delete the folder that I'm in that will generate an error, but it won't generate
an error until it's tried to delete everything in the folder.
This is how I quickly delete the contents of a folder.
If I've got a lot of stuff in a folder
and I just want to delete it all, del /s /q . is the way to do that.
Again, we'll get an error at the end that says you can't delete the folder
you're in, but everything else will be gone except when it's not.
And that's what's going to happen here.
So we've got all these error messages
again, because we can't delete a bunch of folders that are
somewhere in the folder tree that we're trying to delete.
So nothing has changed here.
We're still in folder I can't delete.
We don't know what's causing this problem.
It's often the case where we won't know specifically what's causing the problem.
What I will sometimes do is now go down
the folder tree to one of the subfolders that's part way down here.
I've got a couple of folders that have both have files within them.
One of the things I will do is I will go move one of the folders to backslash,
which is essentially the root of this drive.
I'm going to move it to \t.
So I'm actually moving this folder that is
a subfolder within the subfolder T up to the top of the folder.
Now it moved the thing, right.
So I've just got the one thing left here.
And if I go back to T and correctly type Dir.
You'll see that I now have this other
folder, Wasart Supply in this case up here and I can in fact try
again to delete that folder and everything in it.
Of course this time it worked.
So maybe, maybe we have a length problem
because there was nothing else that prevented that folder from being deleted.
This time the only thing I changed was its
location and I moved it so that it was in a shorter path.
That apparently was enough to make a difference.
The other thing that can happen is,
again with file names causing problems, sometimes that will be an issue.
So now I'm going to go ahead and go further down the folder this time.
You can see this is a very long path which would lend some credence to there
being potentially a path length issue here.
But in this folder I've got a couple of files and I'm going to assume
for a moment that one of them has a bad character in it.
So if I were to just try to delete
the file, that delete would fail all by itself because of this invalid character.
One of the ways to deal with that, to try to deal with that is to rename it.
So I'm going to take one of these first files here rename it to t.t.
This did not work.
This implies to me that there is something
about that file name that is causing a problem.
My guess would be the square brackets
because I don't believe square brackets can be in a file name.
Sometimes renaming this would work.
In this case it didn't.
But it is something to at least have in your toolbox to try.
So if we assume that this didn't work,
if this isn't going to work for us, what then?
Well, then we get to some more obscure techniques that I'm not going to actually
show you but I'm at least going to mention here.
And you'll see why I wrote that previous
article about things, maybe, not being worth taking the time.
One of the things I actually go to first
is to actually boot from a Linux Live CD or USB stick.
The interesting things about Linux is that honestly it does a better job
of handling random characters and file names.
It's easier to escape, it's easier to specify.
And in many cases the set of illegal characters aren't quite the same.
So sometimes the characters that are
causing you a problem in Windows won't in Linux.
So if you were able to boot up into Linux and examine the hard disk,
examine whatever it is you can't delete using the Linux version of its
File Explorer, you may very well find that you can delete the file right there.
Back in Windows we moved a folder to create a shorter path to that folder.
Another approach is to use the subst command which allows you to specify
a drive letter that represents something further down a folder tree that could then
allow you to delete based on that drive letter instead of the full original path.
PowerShell is absolutely something to try.
It's a different tool, it has a different set of base assumptions
and it may behave differently for some of these situations.
Another option is to use the Windows subsystem for Linux.
It's available in the Microsoft Store
and what it will allow you to do is fire up a Linux shell within Windows and then
use Linux commands like RM to delete the file.
The interesting thing about Linux, and this is the same that would be true if
you had booted from a Linux Live CD is the difference in the way they specify
escaped characters or quote non standard characters and the number of characters
they'll actually allow you to have in a file name.
That too can be another way to basically
bypass some of the limitations that whatever Windows tool you're using is
bumping up against and see if that won't work.
Sys internals has a couple of tools called pen moves and move file.
Those are intended to delete files or move files that happen to be in use.
They basically schedule that movement to happen on your next reboot.
The same technique can sometimes be used to resolve untreatable folder issues.
And finally, yeah, there are third party tools, third party tools that will try
and do exactly some of the things we've done here just in a nicer interface.
My recommendation is that you focus on the things you already have available
to you in Windows and ultimately my real recommendation of course is that you not
waste time on this unless it's very very important.
But now you can see here are a number
of different techniques, a number of different ways to go about
deleting files and folders that normally you might not be able to delete.
Hope that was helpful.
For comments, comments for updates,
for related links and more visit ask.leo.com/148863
I'm Leo Notenboom.
And this is Askleo.com. Thanks for watching.