Remove all files in folder windows

I use Windows. I want to delete all files and folders in a folder by system call. I may call like that: >rd /s /q c:destination >md c:destination Do you know an easier way?

I use Windows.

I want to delete all files and folders in a folder by system call.

I may call like that:

>rd /s /q c:destination
>md c:destination

Do you know an easier way?

rene's user avatar

rene

40.8k78 gold badges118 silver badges149 bronze badges

asked Oct 1, 2009 at 9:32

ufukgun's user avatar

2

No, I don’t know one.

If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:

del /q destination*
for /d %x in (destination*) do @rd /s /q "%x"

This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).

Note that within a batch file you need to double the % within the for loop:

del /q destination*
for /d %%x in (destination*) do @rd /s /q "%%x"

answered Oct 1, 2009 at 9:41

Joey's user avatar

JoeyJoey

339k83 gold badges680 silver badges679 bronze badges

12

del c:destination*.* /s /q worked for me. I hope that works for you as well.

ra.'s user avatar

ra.

1,79814 silver badges15 bronze badges

answered Oct 11, 2012 at 19:45

Sean's user avatar

SeanSean

4634 silver badges2 bronze badges

5

I think the easiest way to do it is:

rmdir /s /q "C:FolderToDelete"

The last «» in the path is the important part.

This deletes the folder itself. To retain, add mkdir "C:FolderToDelete" to your script.

AlainD's user avatar

AlainD

4,8563 gold badges37 silver badges90 bronze badges

answered Nov 20, 2014 at 10:26

Banan's user avatar

BananBanan

4334 silver badges2 bronze badges

6

Yes! Use Powershell:

powershell -Command "Remove-Item 'c:destination*' -Recurse -Force"

answered Feb 16, 2017 at 15:00

Rosberg Linhares's user avatar

Rosberg LinharesRosberg Linhares

3,3891 gold badge31 silver badges33 bronze badges

1

If the subfolder names may contain spaces you need to surround them in escaped quotes. The following example shows this for commands used in a batch file.

set targetdir=c:example
del /q %targetdir%*
for /d %%x in (%targetdir%*) do @rd /s /q ^"%%x^"

answered Jan 24, 2014 at 10:05

fractor's user avatar

fractorfractor

1,4942 gold badges14 silver badges30 bronze badges

To delete file:

del PATH_TO_FILE

To delete folder with all files in it:

rmdir /s /q PATH_TO_FOLDER

To delete all files from specific folder (not deleting folder itself) is a little bit complicated. del /s *.* cannot delete folders, but removes files from all subfolder. So two commands are needed:

del /q PATH_TO_FOLDER*.*
for /d %i in (PATH_TO_FOLDER*.*) do @rmdir /s /q "%i"

You can create a script to delete whatever you want (folder or file) like this mydel.bat:

@echo off
setlocal enableextensions

if "%~1"=="" (
    echo Usage: %0 path
    exit /b 1
)

:: check whether it is folder or file
set ISDIR=0
set ATTR=%~a1
set DIRATTR=%ATTR:~0,1%
if /i "%DIRATTR%"=="d" set ISDIR=1

:: Delete folder or file
if %ISDIR%==1 (rmdir /s /q "%~1") else (del "%~1")
exit /b %ERRORLEVEL%

Few example of usage:

mydel.bat "pathtofolder with spaces"
mydel.bat pathtofile_or_folder

answered Nov 10, 2016 at 17:44

Maxim Suslov's user avatar

Maxim SuslovMaxim Suslov

4,0111 gold badge30 silver badges29 bronze badges

One easy one-line option is to create an empty directory somewhere on your file system, and then use ROBOCOPY (http://technet.microsoft.com/en-us/library/cc733145.aspx) with the /MIR switch to remove all files and subfolders. By default, robocopy does not copy security, so the ACLs in your root folder should remain intact.

Also probably want to set a value for the retry switch, /r, because the default number of retries is 1 million.

robocopy "C:DoNotDelete_UsedByScriptsEmptyFolder" "c:tempMyDirectoryToEmpty" /MIR /r:3

answered May 27, 2014 at 15:43

BateTech's user avatar

BateTechBateTech

5,6423 gold badges19 silver badges31 bronze badges

I had an index folder with 33 folders that needed all the files and subfolders removed in them. I opened a command line in the index folder and then used these commands:

for /d in (*) do rd /s /q "%a" & (
md "%a")

I separated them into two lines (hit enter after first line, and when asked for more add second line) because if entered on a single line this may not work. This command will erase each directory and then create a new one which is empty, thus removing all files and subflolders in the original directory.

SteveTurczyn's user avatar

SteveTurczyn

35.5k6 gold badges40 silver badges53 bronze badges

answered Jul 8, 2014 at 19:43

Ynotinc's user avatar

0

Community's user avatar

answered Jun 20, 2016 at 17:41

NoWar's user avatar

NoWarNoWar

35.7k78 gold badges317 silver badges490 bronze badges

1

It takes 2 simple steps. [/q means quiet, /f means forced, /s means subdir]

  1. Empty out the directory to remove

    del *.* /f/s/q  
    
  2. Remove the directory

    cd ..
    rmdir dir_name /q/s
    

See picture

answered May 11, 2020 at 21:51

Jenna Leaf's user avatar

Jenna LeafJenna Leaf

2,20321 silver badges29 bronze badges

1

try this, this will search all MyFolder under root dir and delete all folders named MyFolder

for /d /r "C:Userstest" %%a in (MyFolder) do if exist "%%a" rmdir /s /q "%%a"

Paul Roub's user avatar

Paul Roub

36.2k27 gold badges82 silver badges89 bronze badges

answered Jul 14, 2020 at 17:57

Shailesh Tiwari's user avatar

del .*

This Command delete all files & folders from current navigation in your command line.

answered Nov 14, 2020 at 9:11

Yuvraj Hinger's user avatar

I want to remove all files from a folder structure, so I’m left with an empty folder structure.

Can this be achieved in either batch or VBScript scripting?

I have tried a very basic batch command, but this required the user to allow the deletion of each file. This wasn’t a suitable solution as there are many hundreds of files and this will increase massively over time.

What can you suggest?

Peter Mortensen's user avatar

asked Apr 15, 2014 at 12:40

RobN's user avatar

6

This can be accomplished using PowerShell:

Get-ChildItem -Path C:Temp -Include *.* -File -Recurse | foreach { $_.Delete()}

This command gets each child item in $path, executes the delete method on each one, and is quite fast. The folder structure is left intact.

If you may have files without an extension, use

Get-ChildItem -Path C:Temp -Include * -File -Recurse | foreach { $_.Delete()}

instead.

It appears the -File parameter may have been added after PowerShell v2. If that’s the case, then

Get-ChildItem -Path C:Temp -Include *.* -Recurse | foreach { $_.Delete()}

It should do the trick for files that have an extension.

If it does not work, check if you have an up-to-date version of Powershell

Community's user avatar

answered Apr 15, 2014 at 13:18

MDMoore313's user avatar

MDMoore313MDMoore313

5,8461 gold badge26 silver badges31 bronze badges

13

Short and sweet PowerShell. Not sure what the lowest version of PS it will work with.

Remove-Item c:Tmp* -Recurse -Force

tobassist's user avatar

answered Nov 2, 2017 at 17:21

Evan Nadeau's user avatar

Evan NadeauEvan Nadeau

1,2031 gold badge7 silver badges2 bronze badges

7

You can do so with del command:

dir C:folder
del /S *

The /S switch is to delete only files recursively.

answered Apr 15, 2014 at 12:44

ek9's user avatar

ek9ek9

3,3253 gold badges19 silver badges20 bronze badges

8

Using PowerShell:

Get-ChildItem -Path c:temp -Include * | remove-Item -recurse 

bwDraco's user avatar

bwDraco

45.3k43 gold badges163 silver badges202 bronze badges

answered Oct 28, 2014 at 15:39

Gregory Suvalian's user avatar

Reading between the lines on your original question I can offer an alternative BATCH code line you can use. What this will do when ran is only delete files that are over 60 days old. This way you can put this in a scheduled task and when it runs it deletes the excess files that you don’t need rather than blowing away the whole directory. You can change 60 to 5 days or even 1 day if you wanted to. This does not delete folders.

forfiles -p "c:pathtofiles" -d -60 -c "cmd /c del /f /q @path"

answered Apr 15, 2014 at 13:18

Travis's user avatar

TravisTravis

1,0548 silver badges16 bronze badges

2

Use PowerShell to Delete a Single File or Folder. Before executing the Delete command in powershell we need to make sure you are logged in to the server or PC with an account that has full access to the objects you want to delete.

With Example: http://dotnet-helpers.com/powershell-demo/how-to-delete-a-folder-or-file-using-powershell/

Using PowerShell commnads to delete a file

Remove-Item -Path «C:dotnet-helpersDummyfiletoDelete.txt»

The above command will execute and delete the “DummyfiletoDelete.txt” file which present inside the “C:dotnet-helpers” location.

Using PowerShell commnads to delete all files

Remove-Item -Path «C:dotnet-helpers*.*»

Using PowerShell commnads to delete all files and folders

Remove-Item -Path «C:dotnet-helpers*.*» -recurse

-recurse drills down and finds lots more files. The –recurse parameter will allow PowerShell to remove any child items without asking for permission. Additionally, the –force parameter can be added to delete hidden or read-only files.

Using -Force command to delete files forcefully

Using PowerShell command to delete all files forcefully

Remove-Item -Path «C:dotnet-helpers*.*» -Force

Adil H. Raza's user avatar

answered May 10, 2018 at 7:13

thiyagu selvaraj's user avatar

Try this using PowerShell. In this example I want to delete all the .class files:

Get-ChildItem '.FOLDERNAME' -include *.class -recurse | foreach ($_) {remove-item $_.FullName}

Peter Mortensen's user avatar

answered Nov 20, 2014 at 21:28

Bacara's user avatar

BacaraBacara

1112 bronze badges

  1. In Windows Explorer select the root dir containing all the files and folders.

  2. Search for *

  3. Sort by Type (All the folders will be at the top and all the files listed underneath)

  4. Select all the files and press Delete.

This will delete all the files and preserve the directory structure.

answered Mar 16, 2015 at 9:12

Emel's user avatar

Delete all files from current directory and sub-directories but leaving the folders structure.

(/Q) switch is for asking the user if he is ok to delete

Caution : try it without the /Q to make sure you are not deleting anything precious.

del /S * /Q 

answered May 18, 2015 at 11:30

hdoghmen's user avatar

This is the easiest way IMO

Open PowerShell, navigate to the directory (cd), THEN

ls -Recurse * | rm

(Poof) everything is gone…

If you want to delete based on a specific extension

ls -Recurse *.docx | rm

ls is listing the directory

-Recurse is a flag telling powershell to go into any sub directories

* says everything

*.doc everything with .doc extension

| feed the output from the left to the right

rm delete

All the other answers appear to make this more confusing than necessary.

answered Oct 12, 2016 at 20:45

Kellen Stuart's user avatar

Kellen StuartKellen Stuart

5282 gold badges8 silver badges17 bronze badges

We can delete all the files in the folder and its sub folders via the below command.

Get-ChildItem -Recurse -Path 'D:Powershell Practice' |Where-Object{$_.GetType() -eq [System.IO.FileInfo]} |Remove-Item

Or

Get-ChildItem -Recurse -Path 'D:Powershell Practice' -File | Remove-Item

answered Mar 1, 2020 at 6:34

Dishant Batra's user avatar

1

dir C:testx -Recurse -File | rd -WhatIf
What if: Performing the operation "Remove File" on target "C:testxx.txt".
What if: Performing the operation "Remove File" on target "C:testxblax.txt".

bertieb's user avatar

bertieb

7,23436 gold badges40 silver badges52 bronze badges

answered Apr 10, 2017 at 10:36

With Powershell 5.1:


$extensions_list = Get-ChildItem -Path 'C:folder_path' -Recurse

foreach ( $extension in $extensions_list) {

    if ($extension.Attributes -notlike "Directory") {

        Remove-Item $extension.FullName  
    }
}

It’s removes all itens that are not Directory.

$extension.FullName = Item Path

$extension.Attributes = Item Type ( Directory or Archive )

answered Feb 4, 2020 at 17:43

Maycon Paixão's user avatar

PowerShell example.

I found some paths don’t play nicely, so using -LiteralPath works in all cases.

Help on which can be found on the MS docs for Remove-Item.

# To delete all files within a folder and its subfolders.

$subDir = "Z:a path tosomewhere"

# Remove the -WhatIf and -Verbose here once you're happy with the result.
Get-ChildItem -LiteralPath $subDir -File -Recurse | Remove-Item -Verbose -WhatIf

answered Jun 11, 2021 at 22:54

Ste's user avatar

SteSte

1,0711 gold badge9 silver badges22 bronze badges

If all your files have an extension and none of your folders have a dot, you can do the following:

Remove-Item C:Test* -Include *.* -Recurse

Check the official documentation which provides multiples examples.

answered Feb 17, 2022 at 10:53

Nereis's user avatar

NereisNereis

1012 bronze badges

As a complement to the above answers, actually there’s no need to use the Get-Childitem and pass the result to the pipeline in the above answers, because the -Include keyword is included in the Remove-Item command

One can simply:

Remove-Item -Include «.» «C:Temp» -Recurse

answered Oct 14, 2018 at 14:59

Sams0nT's user avatar

cmd Delete Folder – How to Remove Files and Folders in Windows

Sometimes it’s just faster to do things with the command line.

In this quick tutorial we’ll go over how to open Command Prompt, some basic commands and flags, and how to delete files and folders in Command Prompt.

If you’re already familiar with basic DOS commands, feel free to skip ahead.

How to open Command Prompt

To open Command Prompt, press the Windows key, and type in «cmd».

Then, click on «Run as Administrator»:

Screenshot showing how to open Command Prompt as an administrator

After that, you’ll see a Command Prompt window with administrative privileges:

command-prompt-new-window

Screenshot of Command Prompt window

If you can’t open Command Prompt as an administrator, no worries. You can open a normal Command Prompt window by clicking «Open» instead of «Run as Administrator».

The only difference is that you may not be able to delete some protected files, which shouldn’t be a problem in most cases.

How to delete files with the del command

Now that Command Prompt is open, use cd to change directories to where your files are.

I’ve prepared a directory on the desktop called Test Folder. You can use the command tree /f to see a, well, tree, of all the nested files and folders:

Screenshot after running tree /f in target directory

To delete a file, use the following command: del "<filename>".

For example, to delete Test file.txt, just run del "Test File.txt".

There may be a prompt asking if you want to delete the file. If so, type «y» and hit enter.

Note: Any files deleted with the del command cannot be recovered. Be very careful where and how you use this command.

After that, you can run tree /f to confirm that your file was deleted:

Screenshot after deleting file with del command

Also, bonus tip – Command Prompt has basic autocompletion. So you could just type in del test, press the tab key, and Command Prompt will change it to del "Test File.txt".

How to force delete files with the del command

Sometimes files are marked as read only, and you’ll see the following error when you try to use the del command:

Screenshot of error after trying to delete a read only file

To get around this, use the /f flag to force delete the file. For example, del /f "Read Only Test File.txt":

Screenshot after deleting file with the force flag

How to delete folders with the rmdir command

To delete directories/folders, you’ll need to use the rmdir or rd command. Both commands work the same way, but let’s stick with rmdir since it’s a bit more expressive.

Also, I’ll use the terms directory and folder interchangeably for the rest of the tutorial. «Folder» is a newer term that became popular with early desktop GUIs, but folder and directory basically mean the same thing.

To remove a directory, just use the command rmdir <directory name>.

Note: Any directories deleted with the rmdir command cannot be recovered. Be very careful where and how you use this command.

In this case I want to remove a directory named Subfolder, so I’ll use the command rmdir Subfolder:

Screenshot of a directory not empty error

But, if you remember earlier, Subfolder has a file in it named Nested Test File.

You could cd into the Subfolder directory and remove the file, then come back with cd .. and run the rmdir Subfolder command again, but that would get tedious. And just imagine if there were a bunch of other nested files and directories!

Like with the del command, there’s a helpful flag we can use to make things much faster and easier.

How to use the /s flag with rmdir

To remove a directory, including all nested files and subdirectories, just use the /s flag:

Screenshot after running rmdir with the /s flag

There will probably be a prompt asking if you want to remove that directory. If so, just type «y» and hit enter.

And that’s it! That should be everything you need to know to remove files and folders in the Windows Command Prompt.

All of these commands should work in PowerShell, which is basically Command Prompt version 2.0. Also, PowerShell has a bunch of cool aliases like ls and clear that should feel right at home if you’re familiar with the Mac/Linux command line.

Did these commands help you? Are there any other commands that you find useful? Either way, let me know over on Twitter.



Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Some folders and files are impossible to delete using Windows Explorer. These include files with long paths, names or reserved names like CON, AUX, COM1, COM2, COM3, COM4, LPT1, LPT2, LPT3, PRN, NUL etc. You will get an Access Denied error message when you try to delete these files using Windows Explorer, even if you are an administrator.

Regardless of the reason, these can only be force deleted using command line only. This article explains using cmd to delete folder or file successfully.

Table of contents

  • Before we begin
  • How to remove files and folders using Command Prompt
    • Del/Erase command in cmd
    • Rmdir /rd command in cmd
    • Delete multiple files and folders
    • Delete files and folders in any directory
    • Check the existence of file or folder then remove using IF command
  • How to remove files and folders using Windows PowerShell
    • Delete multiple files and folders
    • Delete files and folders in any directory
  • Delete files and folders with complex and long paths using the command line
  • Closing words

Before we begin

Here are some important things for you to understand before we dig into removing files and folders using Command Prompt and Windows PowerShell. These tips will help you understand the terms and some basic rules of the commands that will be used further in the article.

The most important thing to remember here is the syntax of the path and file/folder name. When typing file name, notice whether there is a gap (space) in it. For example, if the folder name has no space in it, it can be written as-is. However, if there is a gap in it, it will need to be written within parenthesis (“”). Here is an example:

CLI naming syntax

Another thing to remember is that you might see different outcomes while removing folders that are already empty, and folders that have some content in them. Having said that, you will need to use the dedicated options in the command to remove content from within a folder along with the main folder itself. This is called a recursive action.

Furthermore, you must also know how to change your working directory when inside a Command Line Interface. Use the command cd to change your directory, followed by the correct syntax. Here are some examples:

One last thing that might come in handy is being able to view what content is available in the current working directory. This is especially helpful so that you type in the correct spelling of the target file or folder. To view the contents of the current working directory in Command Prompt and PowerShell, type in Dir.

dir

Now that we have the basic knowledge, let us show you how you can delete files and folders using the command line on a Windows PC.

By default, there are 2 command-line interfaces built into Windows 10 – Command Prompt and Windows PowerShell. Both of these are going to be used‌ ‌to‌ ‌delete‌ ‌content‌ ‌from‌ ‌a‌ ‌computer.

How to remove files and folders using Command Prompt

Let us start with the very basic commands and work our way up from there for Command Prompt. We recommend that you use Command Prompt with administrative privileges so that you do not encounter any additional prompts that you already might have.

Del/Erase command in cmd

Del and Erase commands in Command Prompt are aliases of one another. Meaning, both perform the same function regardless of which one you use. These can be used to remove individual items (files) in the current working directory. Remember that it cannot be used to delete the directories (folders) themselves.

Use either of the following commands to do so:

Tip: Use the Tab button to automatically complete paths and file/folder names.

Del File/FolderName

Erase File/FolderName

Replace File/FolderName with the name of the item you wish to remove. Here is an example of us removing files from the working directory:

del erase cmd

If you try to remove items from within a folder, whether empty or not, you will be prompted for a confirmation action, such as the one below:

confirmation yes no

In such a scenario, you will need to enter Y for yes and N for no to confirm. If you select yes, the items directly within the folder will be removed, but the directory (folder) will remain. However, the subdirectories within the folder will not be changed at all.

This problem can be resolved by using the /s switch. In order to remove all of the content within the folder and its subdirectories, you will need to add the recursive option in the command (/s). The slash followed by “s” signifies the recursive option. Refer to the example below to fully understand the concept:

We will be using the Del command here to recursively remove the text files within the folder “Final folder,” which also has a subdirectory named “Subfolder.” Subfolder also has 2 sample text files that we will be recursively removing with the following command:

Del /s "Final folder"

Here is its output:

recursive del

As you can see in the image above, we had to enter “y” twice – once for each folder. with each confirmation, 2 text files were removed, as we had stated earlier in this example. However, if we use File Explorer, we can still see that both the directories – “Final folder” and “Subfolder” – are still there, but the content inside them is removed.

You can also make another tweak to the command so that it is executed silently and you will not be prompted for confirmation. Here is how:

Del /s /q "Final folder"

The /q illustrates that the action be taken quietly.

quiet del

Rmdir /rd command in cmd

Similar to Del and Erase, rmdir and rd are also aliases for one another, which means to remove directory. These commands are used to remove the entire directory and subdirectories (recursively) including their contents. Use the command below to do so:

rmdir "New Folder"

rmdir cmd

The above command will remove the “New folder” only if it is empty. If a folder has subdirectories, you might get the following prompt:

directory is not empty

In this case, we will need to apply the option for recursive deletion of items as we have done earlier with the Del command.

rmdir /s "Final folder"

rmdir recursive

Of course, this can also be performed with the /q option so that you are not prompted with a confirmation.

rmdir /s /q "Final folder"

rmdir recursive quiet

Delete multiple files and folders

Up until now, we have completed the task of deleting single items per command. Now let’s see how you can remove multiple selective files or folders. Use the command below to do so:

For files:

Del "File1.txt" "File3.txt" "File5.txt"

del multiple files

For directories:

rd "Folder1" "Folder3" "Folder5"

rd multiple folders

Here is a before and after comparison of the directory where both of the above commands were executed:

before vs after

You can also use an asterisk (*) concatenated with a file type or file name to perform bulk removal of files with the Del command. However, Microsoft has removed the support for the use of asterisks with rmdir so that users do not accidentally remove entire folders.

Here is an example of us removing all .txt files from our current working directory:

Del "*.txt"

del asterisk

Delete files and folders in any directory

We are working on removing content within the current working directory. However, you can also use the commands we have discussed till now to remove files and folders from any directory within your computer.

Simply put the complete path of the item you want to delete in enclosed parenthesis, and it shall be removed, as in the example below:

different directory rmdir

Check the existence of file or folder then remove using IF command

We have already discussed that you can view the contents of the working directory by typing in Dir in Command Prompt. However, you can apply an “if” condition in Command Prompt to remove an item if it exists. If it will not, the action would not be taken. Here is how:

if exist File/FolderName (rmdir /s/q File/FolderName)

Replace File/FolderName in both places with the name of the item (and extension if applicable) to be deleted. Here is an example:
if exist Desktop (rmdir /s/q Desktop)

cmd if

How to remove files and folders using Windows PowerShell

The commands in Windows PowerShell to delete and remove content from your PC are very much similar to those of Command Prompt, with a few additional aliases. The overall functionality and logic are the same.

We recommend that you launch Windows PowerShell with administrative privileges before proceeding.

The main thing to note here is that unlike Command Prompt, all commands can be used for both purposes – removing individual files as well as complete directories. We ask you to be careful while using PowerShell to delete files and folders, as the directory itself is also removed.

The good thing is that you do not need to specify recursive action. If a directory has sub-directories, PowerShell will confirm whether you wish to continue with your deletion, which will also include all child objects (subdirectories).

Here is a list of all the commands/aliases that can be used in PowerShell to remove an item:

  • Del
  • Rm-dir
  • remove-item
  • Erase
  • Rd
  • Ri
  • Rm

We tested all of these commands in our working directory and each of them was successful in deleting the folders as well as individual items, as can be seen below:

PS all

As can be seen above, the syntax of all the aliases is the same. You can use any of the commands below to delete an item using PowerShell:

Del File/FolderName
Rm-dir File/FolderName
remove-item File/FolderName
Erase File/FolderName
Rd File/FolderName
Ri File/FolderName
Rm File/FolderName

Delete multiple files and folders

You can also delete multiple selective files and folders just as we did while using Command Prompt. The only difference is that you will need to provide the complete path of each item, even if you are in the same working directory. Use the command below to do so:

Del "DriveLetter:PathItemName", "DriveLetter:PathItemName"

Remember to append the file type if the item is not a directory (.txt, .png, etc.), as we have done in the example below:

PS delete selective 1

You can also use an asterisk (*) concatenated with a file type or file name to perform bulk removal of files with the Del command, as done in Command Prompt. Here is an example:

PS asterisk

The command shown above will remove all.txt files in the directory “New folder.”

Delete files and folders in any directory

You can also remove an item in a different directory, just like we did in Command Prompt. Simply enter the complete path to the item in PowerShell, as we have done below:

PS different location

Delete files and folders with complex and long paths using the command line

Sometimes you may encounter an error while trying to delete an item that may suggest that the path is too long, or the item cannot be deleted as it is buried too deep. Here is a neat trick you can apply using both Command Prompt and PowerShell to initially empty the folder, and then remove it using any of the methods above.

Use the command below to copy the contents of one folder (which is empty) into a folder that cannot be deleted. This will also make the destination folder empty, hence making it removable.

robocopy "D:EmptyFolder" D:FolderToRemove /MIR

In this scenario, the EmptyFolder is the source folder that we have deliberately kept empty to copy it to the target folder “FolderToRemove.”

robocopy

You will now see that the folder that was previously unremovable is now empty. You can proceed to delete it using any of the methods discussed in this article.

Closing words

The command line is a blessing for Windows users. You can use any of these commands to remove even the most stubborn files and folders on your computer.

Let us know which solution worked for you in the comments section down below.

Also see:

Subhan Zafar is an established IT professional with interests in Windows and Server infrastructure testing and research, and is currently working with Itechtics as a research consultant. He has studied Electrical Engineering and is also certified by Huawei (HCNA & HCNP Routing and Switching).

Updated: 02/07/2022 by

Delete file image

Now and then, it is a good idea to clean up your drives and delete duplicate photos, documents, temporary files, shortcuts, videos, or other unneeded or unused files or folders. The steps to delete a computer file, directory, or folder vary on the method you want to use and your operating system. To proceed, choose from the list of options below and follow the instructions.

How to delete files in Microsoft Windows

Microsoft Windows users can delete an unwanted file or folder (directory) from a hard drive or external disk drive using many different methods. Below are the more common methods for deleting a file or folder in Microsoft Windows.

Note

Users not familiar with Windows should realize that if you delete a folder or directory, all files and folders in that folder or directory are deleted.

Delete key

Locate the item you want to delete, highlight it by left-clicking the file or folder with your mouse once, and press Delete. You can browse the location of the file or folder using My Computer or Windows Explorer.

Tip

You can delete multiple files or folders by holding down Ctrl and clicking each file or folder before pressing Delete.

Tip

You can hold down Shift while pressing Delete to prevent files from going to the Recycle Bin when deleted.

Right-click delete a file in Windows

Delete file or folder by right-clicking

Open My Computer or Windows Explorer. We recommend you make sure the directory or folder is empty before proceeding, unless you intend to delete everything in it. Locate the file or folder you want to delete and right-click it. Choose the Delete option from the pop-up menu.

How to delete from the local disk

Important

The local disk contains files and folders that are imperative for your computer to run correctly. Unless you know what you are deleting, please do not delete any files from this section.

Open My Computer or Windows Explorer. On the left side of the screen, click This PC. On the right side of the screen, locate and double-click the local disk (usually C: or D:). Double-click the folder containing the file you want to delete. Select the file or folder you want to delete, click File in the top menu bar, and select Delete.

How to delete from an external drive

To delete from a USB flash drive, floppy drive, memory card, or external hard drive, open My Computer or Windows Explorer. On the left side of the screen, click This PC. On the right side of the screen, locate and double-click the drive, which is labeled as USB, flash drive, external hard drive, or the manufacturer’s name. Select the file or folder you want to delete, click File in the top menu bar, and select Delete.

Delete from the file menu

Open My Computer or Windows Explorer. Locate and select the file or folder you want to delete, click File in the top menu bar, and select Delete.

Tip

If the File menu is not visible in My Computer or Windows Explorer, press Alt to make the menu bar visible, including the file menu.

Problems during delete

Some documents and folders may be protected from deletion through encryption or password protection. In this case, you may be asked for a password to decrypt or remove the password protection.

A file may be set as read-only, meaning the user can only open it for viewing and not modify or delete it. When trying to delete a read-only file, you get a message stating the file is write-protected and cannot be deleted. You need to modify or write permissions to delete the file.

Some files may only be deleted with administrator permissions. To delete these files, you would need to have administrator rights on the computer. If you are using a work computer, the technical support staff often are the only users with administrator rights on the computer.

Another possible cause of problems with deleting a file or folder is a virus or malware infection. Viruses and malware can prevent files or folders from being modified or deleted. If this is the case, you need to remove the virus or malware infection to delete the affected file or folder.

  • Unable to delete file: being used by another person or program.
  • How to remove a virus and malware from my computer.

Windows command line

See the MS-DOS and Windows command line section below for information about deleting a file or folder at the Windows command line.

Uninstalling a program

See our uninstalling a program steps for help with uninstalling (deleting) software programs from the computer.

  • How to uninstall software in Windows.

How to restore a deleted file or folder

If you’ve deleted a file by mistake, you can see our page on how to restore a deleted file for further information on recovering a deleted file.

  • How to recover missing, lost, or deleted files.

How to delete files in MS-DOS and the Windows command line

Note

Keep in mind that any deleted file or directory in MS-DOS is not sent to the Windows Recycle Bin.

Before you follow any of the steps below, you must get to an MS-DOS prompt or the Windows command line. If you are new to the command line, you may also want to read through the following pages first.

  • How to get to an MS-DOS prompt or Windows command line.
  • How to use the Windows command line (DOS).

del command

Files

MS-DOS users can delete files using the del command. See this page to get additional information and help with this command. Below is an example of how to use this command.

del example.txt

As seen in the example above, when deleting a file, you need to enter the full file name, including the file extension.

Tip

The del command can delete any file.

Delete multiple files

You can also use wildcards to delete multiple files or a group of files, as shown in the example below.

del *.txt

In the example above, this command would delete all text files that end with a .txt file extension.

Tip

The del command can delete any file extension.

Directory

MS-DOS users can delete directories (dir) in MS-DOS using the deltree command or rmdir command. See either of these links for additional information about these commands. Below is an example of how to use this.

rmdir example

Note

If the directory is full or has other subdirectories, you get an error message. To delete a full directory, you need to use a switch with the above example. For example, «rmdir example /s» to remove a full «example» directory. See our deltree command or rmdir command for additional examples and switches.

  • How to delete files in MS-DOS without a prompt.

Deleting a subdirectory

To delete a subdirectory, subfolder, folder within a folder, or directory within a directory, use a command similar to the example below.

rmdir exampletest

In the example above, the «test» directory in the «example» directory is deleted. You could also use the cd command to change the directory to the example directory and then delete the «test» directory using our first example shown above.

How to delete a directory or file name with a space

To delete a directory or file name with a space in the name, you must surround the directory or file name with quotes, as shown below.

del "my example file.txt"
rmdir "my example directory"

In the above examples, we are deleting the file named «my example file.txt» with quotes surrounding the complete file name and extension and removing the «my example directory» directory.

Tip

The rmdir command can delete any file.

  • How to use the Windows command line (DOS).

How to delete files in Linux, Unix, and other variants

rm command

Files

Linux and Unix users can delete files through the console using the rm command. See this page for additional information about this command. Below is an example of how to use this command.

rm example.txt

As seen in the example above, when deleting a file, you need to enter the full file name, including the file extension.

Tip

The rm command can delete any file.

Delete multiple files

You can also use wildcards if you want to delete multiple files, as shown in the example below.

rm *.txt

In the example above, this command would delete all files with a .txt file extension.

Tip

The rm command can delete any file of file extensions.

Directory

Linux and Unix users can delete folders through the console with the rmdir command. See this page for additional information about this command. Below is an example of how to use this command.

rmdir example

Tip

Like Microsoft Windows, with Linux and Unix, you can also delete files through the GUI by locating the file and pressing the delete key on the keyboard.

Deleting a subdirectory

To delete a directory in another directory (subdirectory), use a command similar to the example below.

rmdir exampletest

In the example above, the «test» directory in the «example» directory would be deleted. You could also use the cd command to change the directory to the example directory and then delete the «test» directory using our first example shown above.

How to delete a directory or file name with a space

To delete a directory or file name with a space in the name, you must surround the directory or file name with quotes, as shown below.

rm "my example file.txt"
rmdir "my example directory"

In the examples above, we are deleting the file named «my example file.txt» with quotes surrounding the complete file name and extension. We are also removing the «my example directory» directory.

Tip

The rmdir command can delete any file.

  • How to remove a full directory in Linux.
  • Linux and Unix shell tutorial.

How to delete files on macOS

Apple macOS users can delete photos, documents, or other files or folders (directory) on their Mac using many different methods. Below are the more common methods for deleting a file or folder.

Note

Users not familiar with Apple macOS should realize that it deletes all the files in a folder if you delete that folder.

Delete key

The delete key on the keyboard by itself does not delete a file or folder on macOS. To delete a file or folder, press and hold Command, then press delete. You can browse to the location of the file or folder using Finder.

Right-click and choosing Move to Trash

Open Finder, locate the file or folder you want to delete, and right-click the file or folder. In the right-click menu that appears, click the Move to Trash option.

Delete from the file menu

Open Finder, locate and select the file or folder you want to delete. Click File in the top menu bar and select Move to Trash.

Terminal

To delete files or directories in the Terminal command line, use the rm command.

How to delete files on Microsoft Windows 3.X

File Manager

  1. Open File Manager
  2. Locate the folder or file you want to delete, then click File and Delete.

MS-DOS

See the MS-DOS user section above for information about deleting a directory in MS-DOS.

You might often find unwanted or unnecessary files and folders on your PC that you would want to get rid of. Deleting these files or folders helps to make an extra space on your PC which is always a good thing. In this post meant beginners, we will show you the different ways to delete files and folders in Windows 11/10, permanently or temporarily, by means of keyboard shortcuts, File Explorer, PowerShell, Command Prompt, specialized software, etc.

There are various ways by which you can delete Files and Folders, permanently or temporarily, in Windows 11/10:

  1. Using Context Menu – Right-clicking on the File or Folder
  2. Using Keyboard Shortcut – Delete key
  3. Using Keyboard Shortcut – Shift+Delete key
  4. Drag & Drop to the Recycle Bin
  5. Using File Explorer Ribbon
  6. Using PowerShell
  7. Using Command Prompt
  8. Using Specialized Software

Let us look at all of them in detail.

1] Using Context Menu – Right-clicking on the File or Folder

Windows 11

Cut, Copy, Paste, Rename, Delete, Share Windows 11

When you right-click on any file or folder that you want to carry out a Cut, Copy, Paste, Rename, Delete, Share, you will now no longer see any options mentioned in text.  What you will instead see are icons displayed on the top (or bottom) of the context menu as shown in the image above. These icons stand for:

  • Cut
  • Copy
  • Rename
  • Paste
  • Share
  • Delete.

You have to click on the Bin icon to delete the file or folder..

Windows 10

How to delete Files and Folders in Windows 10

To delete using the Context Menu, select the file(s) or the folder(s) you wish to get rid of. Right-click on it and press the ‘Delete’ option from the pop-up window that opens. The selected items are sent to the Recycle Bin as they are temporarily deleted and can be recovered if required.

Deleting a folder consisting of other files or folders will delete all that is inside that folder, so make sure you check properly before doing so.

2] Using Keyboard Shortcut – Delete key

While you are on your Desktop or File Explorer, select the items you want to delete and press the ‘Delete’ key or ‘Ctrl+D’ on the keyboard. This will temporarily delete the selected file(s) or folder(s) and will be moved to the Recycle Bin. However, these deleted files can be restored from the Recycle Bin.

To select all files in a folder, select ‘CTRL+A’.

3] Using Keyboard Shortcut – Shift+Delete key

To delete files or folders without sending them to the Recycle Bin, select the items and press ‘Shift+Delete’ on the keyboard. This will permanently delete the selected files. In such a case, these deleted files can only be recovered or restored using specialized software like Recuva.

TIP: Add Permanently Delete to Context Menu to Windows

4] Drag & Drop to Recycle Bin

How to delete Files and Folders in Windows 10

This is a simple method of deleting the file(s) or folder(s). All you have to do is select the file, and drag and drop it to the Recycle Bin.

5] Using File Explorer Ribbon

This method will teach you how to temporarily or permanently delete file(s) or folder(s) using the Home Menu from the File Explorer Ribbon.

To begin with, open File Explorer (Win+E) and navigate to the location of the files to be deleted. Tap or select the files you wish to get rid of and then.

In Windows 11 click on the BIn icon to delete the file or folder.

In Windows 10, click on the ‘Home’ tab in the Ribbon above.

How to delete Files and Folders in Windows 10

Further, press ‘Delete’ from the ‘Home’ tab, and you will see that a drop-down menu will open. Press ‘Recycle’ to temporarily delete the file, which will be moved to the Recycle Bin.

To permanently delete the files, select the ‘Permanently delete’ option.

How to permanently delete files and folders in Windows 10

A confirmation dialog box will appear asking if you are sure about permanently deleting this file. Click on ‘Yes’ to proceed.

6] Using PowerShell

If you are unable to delete a file or folder from your computer, you can use Windows PowerShell to delete any file and folder effortlessly. The advantage of using PowerShell is that you can force delete a file and remove all items from inside a folder.

7] Using Command Prompt

You can also delete files & folders using Command Prompt. using the DEL or RD commands.

8] Permanently Delete Files Using Specialized Software

There are specialized software that permanently delete unwanted files from your Windows 10 PC which cannot be recovered by any file recovery tools. Some of these data erasing apps are Eraser, WipeFile, Freeraser, File Shredder, Alternate File Shredder, and more.

You usually have to download and install these apps to use them. These apps permanently delete all the selected files and folders and cannot really be recovered by any specialized recovery tools.

These are all the possible ways to delete File(s) and Folder(s) in Windows 11/10.

In some cases, Windows is not allowing some users to delete their files or folders. You can use the command prompt to delete a file or a folder if you encounter such a problem. In this article, we will show you how exactly you can use this method to delete files and folders.

  • How to delete files on Windows 10 with CMD
  • How to delete folders on Windows 10 with CMD
  • Force Uninstall

Deleting files on Windows 10 with command prompt

The built-in del command can help you delete files on Windows 10. You have to point out the specific path to this file on your PC.

To use this method do the following:

  1. Open the Start Menu and in the search bar type “cmd”. Right-click on the result and select “Run as Administrator”
  2. Type in the field the following command where PATH will be replaced with the full path to the file you want to delete.
    del path
    delete file with command prompt
  3. After that press Enter

Lets’ see this example to clarify the whole process:

You want to delete a file called Info.txt that is located on your desktop. Use the following command in the Command Prompt where you replace username with your own username:

Del “C:UsersusernameDesktopInfo.txt”

After you enter this command the file will be deleted from the Desktop.

There are several commands that you can use to modify a bit the del command.

For instance, you can add the /p parameter to the command to get Command Prompt to be asked for confirmation before deleting a file.

You can also add the /f parameter to force delete a read-only file.

You can also use Revo Uninstallers’ force uninstall feature if you have trouble removing stubborn programs.

To delete folders in Windows 10 with CMD you have to use the rmdir command

This command will remove all folders including the subfolders and files in them.

To use this command do the following:

  1. Open the Start Menu and in the search bar type “cmd”. Right-click on the result and select “Run as Administrator”
  2. Type in the field the following command where PATH will be replaced with the full path to the file you want to delete.
    rmdir PATH
    remove folder with command prompt
  3. Press Enter to finish the process

For example, to delete a folder named “Info” on your desktop, use the following command where you will replace username with your own username:

rmdir “C:UsersusernameDesktopInfo”

After you press Enter the folder named “Info” on your desktop will be deleted.

If you want to delete all the files and subfolders in the folder you want to delete, add the /s parameter. This will ensure that the folder is removed including the subfolders and files in it.

Conclusion

As you’ve noticed, commands to delete files and folders in Windows 10 can be pretty handy if you have trouble deleting them the regular way.

Post Views: 9,291

Like this post? Please share to your friends:
  • Remove access to use all windows update features
  • Removable storage devices папка на рабочем столе windows 10 как удалить
  • Remotemouse net скачать на компьютер windows 10
  • Remixlive скачать полную версию на windows
  • Remix os установка на пк рядом с windows