Windows delete folder not empty

Do you want to delete a directory from Windows command prompt(CMD)? This post explains how to use the command rmdir to delete folders and their contents. You can also find examples for each use case of folder deletion – empty folders, non empty folders, folders with white spaced names etc.

Delete folder from CMD

Run the command rmdir on the folder.

rmdir directoryname

Example:

C:>rmdir emptydir
C:>

How to delete a non empty folder

The simple rmdir does not work for folders having some content.

C:>rmdir nonemptydir
The directory is not empty.

Use /s option to delete the folder contents along with the folder. This deletes all subfolders recursively.

C:>rmdir /S nonemptydir
nonemptydir, Are you sure (Y/N)? y 
C:>

Force delete a folder without confirmation

To  force delete directory, without being asked for confirmation, we can use /Q switch.

rmdir /Q /S nonemptydir

We can also use ‘rd’ in place of ‘rmdir‘. Both names refer to the same command. This command works on Windows 2000, Windows XP, Server 2003, Vista, Windows 7 and 10.

Deleting directory with white spaces in the name

Rmdir can delete files with whitespaces in the name, you just need to wrap up the folder name in double quotes as shown in the below example.

rmdir /Q /S "folder with spaces in the name"

Delete contents of a directory but keep the directory

The usecase here is to delete all the contents of the directory but keep the parent directory so that we do not need to create it again. rmdir /Q /S does not work here as it deletes the parent directory too. Rather the below commands should do the trick.

forfiles /P directory_path /M * /C "cmd /c if @isdir==FALSE del @file"
forfiles /P directory_path /M * /C "cmd /c if @isdir==TRUE rmdir /S /Q @file"

This works in 2 steps – the first command deletes all files, whereas the second one deletes all subdirectories.

Errors

To delete a directory, you should have appropriate access permissions on the directory. Otherwise rmdir throws ‘Access denied’ error.

I am making a batch script and part of the script is trying to remove a directory and all of its sub-directories. I am getting an intermittent error about a sub-directory not being empty. I read one article about indexing being the culprit. I disabled WSearch but I eventually got the error again. Here’s the command:

rmdir /S /Q "C:\<dir>\"

Rose's user avatar

Rose

2,8024 gold badges28 silver badges42 bronze badges

asked Apr 8, 2014 at 21:07

Mayhem's user avatar

1

I experienced the same issues as Harry Johnston has mentioned. rmdir /s /q would complain that a directory was not empty even though /s is meant to do the emptying for you! I think it’s a bug in Windows, personally.

My workaround is to del everything in the directory before deleting the directory itself:

del /f /s /q mydir 1>nul
rmdir /s /q mydir

(The 1>nul hides the standard output of del because otherwise, it lists every single file it deletes.)

answered Sep 16, 2015 at 11:26

BoffinBrain's user avatar

BoffinBrainBoffinBrain

6,3376 gold badges33 silver badges59 bronze badges

6

I’m familiar with this problem. The simplest workaround is to conditionally repeat the operation. I’ve never seen it fail twice in a row — unless there actually is an open file or a permissions issue, obviously!

rd /s /q c:\deleteme
if exist c:\deleteme rd /s /q c:\deleteme

answered Apr 8, 2014 at 22:47

Harry Johnston's user avatar

Harry JohnstonHarry Johnston

35.7k6 gold badges68 silver badges159 bronze badges

6

I just encountered the same problem and it had to do with some files being lost or corrupted. To correct the issue, just run check disk:

chkdsk /F e:

This can be run from the search windows box or from a cmd prompt. The /F fixes any issues it finds, like recovering the files. Once this finishes running, you can delete the files and folders like normal.

answered Feb 2, 2017 at 5:22

jrose's user avatar

jrosejrose

5496 silver badges11 bronze badges

2

enter the Command Prompt as Admin and run

rmdir /s <FOLDER>

answered May 2, 2018 at 15:24

Adilson Cabral's user avatar

1

I had a similar problem, tried to delete an empty folder via windows explorer. Showed me the not empty error, so I thought I try it via admin cmd, but none of the answers here helped.

After I moved a file into the empty folder. I was able to delete the non empty folder

answered Apr 3, 2018 at 11:11

Grisu118's user avatar

Grisu118Grisu118

3391 gold badge3 silver badges7 bronze badges

3

As @gfullam stated in a comment to @BoffinbraiN’s answer, the <dir> you are deleting itself might not be the one which contains files: there might be subdirectories in <dir> that get a «The directory is not empty» message and the only solution then would be to recursively iterate over the directories, manually deleting all their containing files… I ended up deciding to use a port of rm from UNIX. rm.exe comes with Git Bash, MinGW, Cygwin, GnuWin32 and others. You just need to have its parent directory in your PATH and then execute as you would in a UNIX system.

Batch script example:

set PATH=C:\cygwin64\bin;%PATH%
rm -rf "C:\<dir>"

answered Mar 31, 2016 at 14:04

Gobe's user avatar

GobeGobe

2,5691 gold badge25 silver badges24 bronze badges

Im my case i just moved the folder to root directory like so.

move <source directory> c:\

And then ran the command to remove the directory

rmdir c:\<moved directory> /s /q

answered Oct 11, 2015 at 10:05

Daniel Barde's user avatar

Daniel BardeDaniel Barde

2,6135 gold badges31 silver badges40 bronze badges

3

I had «C:\Users\User Name\OneDrive\Fonts», which was mklink’ed ( /D ) to «C:\Windows\Fonts», and I got the same problem. In my case

cd «C:\Users\User Name\OneDrive»

rd /s Fonts

Y (to confirm the action)

helped me. I hope, that it helps you too ;D

answered Dec 8, 2018 at 22:23

Maciej Bledkowski's user avatar

1

What worked for me is the following. I appears like the RMDir command will issue “The directory is not empty” nearly all the time…

:Cleanup_Temporary_Files_and_Folders

Erase /F /S /Q C:\MyDir

RMDir /S /Q C:\MyDir
If  Exist  C:\MyDir  GoTo Cleanup_Temporary_Files_and_Folders

Tamás Sengel's user avatar

Tamás Sengel

56.1k29 gold badges169 silver badges225 bronze badges

answered Jan 17, 2017 at 19:34

user7432246's user avatar

The reason rd /s refuses to delete certain files is most likely due to READONLY file attributes on files in the directory.

The proper way to fix this, is to make sure you reset the attributes on all files first:

attrib -r %directory% /s /d
rd /s %directory%

There could be others such as hidden or system files, so if you want to play it safe:

attrib -h -r -s %directory% /s /d
rd /s %directory%

answered Apr 22, 2020 at 1:59

Peter Hoeg's user avatar

Peter HoegPeter Hoeg

9019 silver badges13 bronze badges

0

one liner:

if exist folder rmdir /Q /S folder

I’m using this in a NPM script like so (Javascript) :

//package.json
  "scripts": {
    "start": "parcel --no-cache",
    "clean": "if exist dist rmdir /Q /S dist",
    "deploy": "npm run clean && parcel build --no-source-maps && firebase deploy"
  },

answered Nov 9, 2021 at 21:54

Alexandre Desroches's user avatar

Open CMD as administrator

chkdsk c: /F /R
  • Press the “Y” key if asked to check your disk the next time your system restarts.

Restart the machine. After that just delete the folder.

answered Mar 16, 2022 at 16:32

KR93's user avatar

KR93KR93

1,12811 silver badges10 bronze badges

Windows sometimes is «broken by design», so you need to create an empty folder, and then mirror the «broken folder» with an «empty folder» with backup mode.

robocopy - cmd copy utility

/copyall - copies everything
/mir deletes item if there is no such item in source a.k.a mirrors source with
destination
/b works around premissions shenanigans

Create en empty dir like this:

mkdir empty

overwrite broken folder with empty like this:

robocopy /copyall /mir /b empty broken

and then delete that folder

rd broken /s
rd empty /s

If this does not help, try restarting in «recovery mode with command prompt» by holding shift when clicking restart and trying to run these command again in recovery mode

Paul Roub's user avatar

Paul Roub

36.3k27 gold badges84 silver badges93 bronze badges

answered Jun 1, 2020 at 19:23

Kristijonas Grigorovičius's user avatar

1

I can think of the following possible causes:

  1. there are files or subdirectories which need higher permissions
  2. there are files in use, not only by WSearch, but maybe by your virus scanner or anything else

For 1.) you can try runas /user:Administrator in order to get higher privileges or start the batch file as administrator via context menu. If that doesn’t help, maybe even the administrator doesn’t have the rights. Then you need to take over the ownership of the directory.

For 2.) download Process Explorer, click Find/Find handle or DLL... or press Ctrl+F, type the name of the directory and find out who uses it. Close the application which uses the directory, if possible.

answered Apr 8, 2014 at 21:24

Thomas Weller's user avatar

Thomas WellerThomas Weller

55.6k20 gold badges126 silver badges223 bronze badges

2

Similar to Harry Johnston’s answer, I loop until it works.

set dirPath=C:\temp\mytest
:removedir
if exist "%dirPath%" (
    rd /s /q "%dirPath%" 
    goto removedir
)

answered May 1, 2017 at 21:46

BuvinJ's user avatar

BuvinJBuvinJ

10.3k5 gold badges83 silver badges96 bronze badges

Force delete the directory (if exists)

Delete.bat

set output_path="C:\Temp\MyFolder"
    
if exist %output_path% (
   echo Deleting %output_path%
   attrib -r /s /d %output_path%
   rd /s /q %output_path%
)

answered Oct 6, 2021 at 9:07

Alper Ebicoglu's user avatar

Alper EbicogluAlper Ebicoglu

8,9741 gold badge51 silver badges55 bronze badges

I’ve fixed this before my making sure there wasn’t extra whitespace in the name of the directory I was deleting. This is more of a concern when I had the directory name contained within a variable that I was passing to RD. If you’re specifying your directly in quotes then this isn’t helpful, but I hope that someone like me comes along with the same problem and sees this. RD /S /Q can work, as I noticed the issue started happening when I changed something in my batch script.

answered Jun 19, 2022 at 7:07

gallopinggoose6's user avatar

Does Microsoft Windows display a “The Directory Is Not Empty” error when you try to delete a folder? Your folder may be locked, or you may not have the required permission to remove the folder. You can follow a few easy methods to fix this problem and delete your folder successfully.

Some reasons you may not be able to remove your folder is that File Explorer is experiencing a minor glitch, your disk has a bad sector, you don’t have permission to remove the folder, or Windows’ system files are corrupt.

Restart File Explorer

When you get a “The Directory Is Not Empty” error, an easy fix you can apply to fix your issue is to restart File Explorer. Doing so resolves any minor problems with your file manager utility, allowing you to delete your items without any problems.

  1. Right-click the Windows taskbar and choose Task Manager.
  2. Right-click Windows Explorer on the list and select Restart.

  1. Try to delete your folder.

Renaming Your Folder to Resolve the Issue

If you still can’t delete your folder, give your folder a new name, and then try to remove the item. This method should resolve the issue if a minor system glitch is causing the problem.

  1. Right-click the problematic folder and choose Rename. Alternatively, select the folder and press F2 on your keyboard.

  1. Enter a new name for your folder and press the Enter key.
  2. Right-click the folder and choose Delete.

Delete the Folder Using the Free Unlocker App

If you still can’t remove your folder, that may be because an app or service on your computer is using that folder. In this case, you can use a free app called Unlocker to remove all locking handles from your folder and delete the folder.

  1. Download and install the free Unlocker app on your PC.
  2. Launch the app and select Yes in the User Account Control prompt.
  3. Choose your problematic folder on the list and select OK.
  4. Unlocker will display the items locking your folder.
  5. Choose Delete from the drop-down menu and select OK.

Delete Files Using Command Prompt

Command Prompt is a good alternative to use when you can’t use File Explorer to remove items. You can use a command in this command-line utility to remove your chosen folder from your storage.

  1. Open the Start menu, find Command Prompt, and select Run as administrator.
  2. Choose Yes in the User Account Control prompt.
  3. Type the following command in CMD and press Enter. Here, replace PATH with the full path to the folder you want to delete.
    rmdir /s “PATH”

Check Disk for Bad Sectors to Fix the Empty Error

The hard disk where you’ve stored your folder may have a bad sector, preventing you from deleting your items. In this case, use Windows’ built-in CHKDSK command to find and fix bad sectors on your disk.

  1. Open Start, locate Command Prompt, and select Run as administrator.
  2. Choose Yes in the User Account Control prompt.
  3. Type the following command in CMD and press Enter. Here, replace C with the drive where you’ve stored the folder.

    CHKDSK /F C:

  1. Type Y on your keyboard and press Enter to unmount the drive.
  2. If you see a message asking you to schedule the fix on the next reboot, type Y and press Enter.
  3. Restart Windows, and CHKDSK will kick in to fix any bad items on your disk.

Get Full Permission for Your Folder to Fix Error

You may not have full permission for your folder, which leads to a “The Directory Is Not Empty” error when you try to remove the folder. You can fix that by taking ownership of your chosen folder.

  1. Right-click your folder and choose Properties.
  2. Select the Security tab and choose Advanced.
  3. Select Change next to Owner.
  4. Type your Windows username in the text box, select Check Names, and choose OK.
  1. Select Apply followed by OK.
  2. Choose OK on the Properties window.
  3. Try to remove your folder.

Run a Full Virus Scan on Your Windows PC

Your Windows PC may have a virus or malware infection, and this item may have gained unauthorized control over your folder, preventing you from deleting your folder. In this case, you can run a full virus check and find and remove all the threats from your PC.

You can use a third-party antivirus program or Windows’ built-in Microsoft Defender Antivirus to scan your computer. Here are the steps for running a full scan using the latter.

  1. Open Start, find Windows Security, and launch the app.
  2. Select Virus & threat protection in the app.
  3. Choose Scan options on the following page.
  4. Select Full scan and choose Scan now.

  1. Wait for your antivirus to finish scanning your PC.

Fix Corrupt Windows Files

Windows’ operating system files may have become corrupt, causing your folder not to delete. In this case, you can use Windows’ built-in SFC (System File Checker) tool to find and fix all corrupt files on your computer.

This tool runs from a Command Prompt window and recognizes and replaces all the faulty system files on your storage.

  1. Open Start, find Command Prompt, and select Run as administrator.
  2. Choose Yes in the User Account Control prompt.
  3. Enter the following on the Command Prompt window and press Enter:

    DISM.exe /Online /Cleanup-image /Restorehealth

  1. Then, use the following command to fix your corrupt files: sfc /scannow
  2. Try to remove the directory.

Restore Your Windows System

If Windows’ “The Directory Is Not Empty” error still doesn’t go away, roll back any recent changes you may have made to your PC. One way to do this is by using Windows’ built-in System Restore tool.

This tool allows you to roll back your system to a restore point created in the past, which helps fix many issues.

  1. Open Start, find Recovery, and select the item in the search results.
  2. Choose Open System Restore on the following page.
  3. Select Next on the tool’s first screen.
  4. Choose the most recent restore point on the list and select Next.

  1. Select Finish to begin restoring your PC.

Finally, You Can Delete Your Folder on Your Windows PC

It quickly gets frustrating when you want to delete a folder but can’t do that as Windows keeps displaying the “The Directory Is Not Empty” error. Luckily, this error isn’t too hard to deal with, and you can use the methods above to resolve the problem.

Once the item locking the folder is gone, you’ll be able to delete your folder without any issues.

The rm -rf command in Linux is used to force the deletion of folders and their contents recursively.

The rmdir command is a Windows rm equivalent in a Windows command prompt (CMD), that also can be used to force the folders deletion with all their contents.

In this note i will show how to force the rmdir command to delete folders and their contents recursively without throwing any errors like “The directory is not empty” or “Access is denied“.

Cool Tip: Windows touch command equivalent in CMD and PowerShell! Read more →

Grep Command in Windows

If you try to delete a non-empty folder from the Windows command prompt (CMD) you will get the error as follows:

The directory is not empty.

To force the rmdir command to delete this folder, execute it as follows:

C:\> rmdir /s /q <folder>

The rmdir command parameters used to force the deletion:

Option Description
/s Delete the folder with all the sub-folders and files in it.
/q Don’t prompt for a confirmation.

If while trying to delete a folder you are getting the “Access is denied“, launch the CMD as an administrator, for this press the ⊞ Win keybutton to open the “Start” menu, type in cmd to search for the Windows command prompt and press the Ctrl + Shift + Enter.

Cool Tip: Delete a file or a folder that is open in another program! Read more →

Was it useful? Share this post with the world!

The error message «Cannot delete folder, directory is not empty» can be faced on any Windows based computer (Windows 10, 8, 7, Vista or XP) if you try to delete a folder that the system cannot access due to the permissions applied to it. In other cases the «Cannot Remove folder because the directory is not empty» issue, can be solved after scanning the hard disk for errors by using the «CHKDSK» command.

Cannot Remove Folder, Directory is not empty

In this tutorial you will find detailed instructions on how to resolve the following errors while deleting a file or folder on Windows 10, 8, 7, Vista or XP:

    • «Cannot remove Folder or File. The Directory is not Empty»
    • «Folder Access Denied. You need permission to perform this action»
    • «Cannot delete file. Cannot read from the source file or disk»
    • «Cannot Remove Folder. Cannot find the specified file»
    • Cannot Delete a File or Folder error in Windows.

How to fix: Cannot Remove Folder or File. The Directory is Not Empty or Access is Denied

Method 1. Run CHKDSK on the drive.

The first step to resolve the ?Cannot remove folder» or «Cannot remove file»  issue(s), is to perform a check disk scan on the disk, that contains the folder that we cannot delete.

1. Open an elevated command prompt (Command Prompt (Admin)).

Cannot Delete Folder Directory is not empty

2. In the command prompt window, type the following command and press Enter: *

  • chkdsk c: /R

* Press the “Y” key if asked to check your disk the next time your system restarts.

chkdsk

3. After disk checking try to delete the problematic folder (or file). If you still cannot remove the folder, proceed to method-2.

Method 2. Modify Folder’s (or File’s) Permissions.

1. In Windows explorer, right click at the folder that you cannot delete and select Properties.
2. Select the Security tab and then click Advanced.

fix error deleting folder

3. Click Change Owner

fix error deleting file

4. In the object name box type, type the name of the Administrator account (e.g. «User1» in this example) and then click OK.

Cannot Delete a File or Folder error in Windows

5. Check the «Replace Owner on subcontainers and objects» checkbox and then click OK & OK again to close the security settings.

Cannot Remove Folder. Cannot find the specified file

6. Now try to delete the folder (or the file) that you cannot delete. If you receive the same message, then proceed to method-3 below.

Method 3. Delete File or Folder from Command Prompt.

1. Open an elevated command prompt (Command Prompt (Admin)).

2. In elevated command prompt, give the below commands, according your case:

A. To delete a folder: *

  • takeown /F «FolderName» /r /d y
  • icacls «FolderName» /grant Username:F /t
  • rd «FolderName» /S /Q

* Notes:
1. Replace the «FolderName» with the full path and the name of the folder that you cannot delete.
2. Replace the «Username» with the username of the Administrator account.

e.g: if you want to delete the «Demo» folder (and its subfolders and files) under the «C:\Program Files\» directory and your Administrator account name is «User1», then the commands should be:

    • takeown /F «C:\Program Files\Demo» /r /d y
    • icacls «C:\Program Files\Demo» /grant User1:F /t
    • rd «C:\Program Files\Demo» /S /Q

B. To delete a file: *

    • takeown /F «Filename»
    • icacls «Filename» /grant Username:F
    • del «Filename»

* Notes:
1. Replace the «Filename» with the full path and the name of the file that you cannot delete.
2. Replace the «Username» with the username of the Administrator account

e.g. if you want to delete the «Example.doc» file under the «C:\Temp» directory and your Admin account name is User1, then the commands should be:

    • takeown /F «C:\Temp\Example.doc»
    • icacls «C:\Temp\Example.doc» /grant User1:F
    • del «C:\Temp\Example.doc»

That’s all folks! Did it work for you?

Please leave a comment in the comment section below or even better: like and share this blog post in the social networks to help spread the word about this solution.

If this article was useful for you, please consider supporting us by making a donation. Even $1 can a make a huge difference for us.

  • Windows desktop themes windows 10
  • Windows default font windows 10
  • Windows delete file from command line
  • Windows debugging tools windows 10 скачать
  • Windows delete file cmd windows