Windows how to delete all files

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

Kris Koishigawa

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»:

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

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:

To delete a file, use the following command: del » » .

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:

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:

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

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.

Читайте также:  Amd64 microsoft windows что это

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 .

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 :

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:

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.

Kris Koishigawa

Read more posts by this author.

If you read this far, tweet to the author to show them you care. Tweet a thanks

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

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

How to Delete a File in Windows (several variants)

This basic guide describes in detail how to delete a file (folder) in Windows (several ways): from simple deletion to guaranteed deletion with protection from data recovery programs.

Contents

Delete file to “Recycle Bin”

💡 To select multiple files or folders, hold the special button and press the left mouse button:

  • Сtrl — highlight a specific file or folder;
  • Shift — selection of a range of files or folders.

How to delete a file (folder) using the menu

  1. Right-click on a document or folder;
  2. Select the Delete item;
  3. Click Yes in the confirmation request.

The file will be deleted to the Recycle Bin.

How to delete a file (folder) using the button

  1. Highlight a document;
  2. Press the Del key;
  3. Click Yes in the confirmation request.
Читайте также:  Как переустановить windows если она слетела

The file will be deleted to the Recycle Bin.

Disable (enable) delete confirmation

  1. Click on the Recycle Bin can icon with the right mouse button;
  2. Select Properties;
  3. Disable the Display delete confirmation dialog.

💡 If you just deleted the desired file, press the key combination Ctrl + Z. This will undo your last action in Windows, the deleted file or folder will return to its place.

How to recovery files from Recycle Bin

  1. Open the Recycle Bin;
  2. Select the desired file or folder;
  3. Right click on the selected file;
  4. Select Recover.

Restore files

The file will be restored to the folder from which it was deleted.

You can transfer the file (or folder) from the Recycle Bin to the folder you need.

Click and hold the left mouse button on the selected file and drag it to the folder or Desktop you need.

How to empty Recycle Bin

You can delete all files from the Recycle Bin.

  1. Click on the Recycle Bin with the right mouse button;
  2. Click Empty Recycle Bin.

Empty Recycle Bin

How to permanent Delete

  1. Highlight a file or folder;
  2. Press the key combination Shift + Del;
  3. Click Yes in the confirmation request.

Permanently delete folder

Guaranteed file deletion

Guaranteed deletion of data on the active disk

For guaranteed data deletion, we use the standard CIPHER utility.

Cipher.exe is a command-line tool (included with Windows 2000) that you can use to manage encrypted data by using the Encrypting File System (EFS)

💡 To overwrite as much data as possible, it is recommended that you close all other applications while running CIPHER / W.

  1. Run the command prompt as administrator;
  2. We launch the CIPHER command with the necessary keys.

/W — key, deletes data from available unused disk space in the entire volume. The content is overwritten in three stages, with values: 00, FF and a random number.

:X — work volume label.

Utility Cipher.exe

Guaranteed data deletion on inactive devices

For guaranteed data deletion on inactive devices: unused hard disks, portable hard disks and flash devices, we use the standard FORMAT utility.

  1. Run the command prompt as administrator;
  2. We start the format command with the necessary keys.

X – specifies the drive letter (followed by a colon), mount point, or volume name.

/P:count — zero every sector on the volume. After that, the volume will be overwritten “count” times using a different random number each time. If “count” is zero, no additional overwrites are made after zeroing every sector.

To run the utility successfully, you must enter the correct label of the active volume/device.

Utility Format.com

The process can take a long time, especially if a large number is indicated in the rewrite counter.

Delete via the Command prompt

How to delete a file via command prompt

  1. Run the command prompt;
  2. Go to the desired folder (cd command);
  3. Run the del command and the fully qualified file name with the extension.

If there are spaces in the file name, enclose the name in quotation marks.

To delete files from system directories, you must run the command prompt as administrator.

How to delete a folder via command prompt

  1. Run the command prompt;
  2. Go to the desired folder;
  3. Run the RD command.
Читайте также:  Презентацию сравнение операционных систем windows

/S – removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

If there are spaces in the directory name, enclose the name in quotation marks.

To delete directory from system directories, you must run the command prompt as administrator.

Delete via PowerShell

The PowerShell command to delete folders and files is Remove-Item.

The synonyms of the Remove-Item command are: del, erase, rd, ri, rm and rmdir, so commands from the Windows command prompt will also work.

Windows PowerShell

X – full path to the file (with extension) or folder.

Recurse – the key indicating the deletion of the content folder.

Comparison table of options how to delete a file

Comparison table on how to delete files and folders.

Deleting data to the Recycle Bin Deleted data can be restored from the Recycle Bin folder on the Desktop.
Permanent data deletion Deleted data can only be restored using special programs.
Guaranteed data deletion Deleted data cannot be recovered even by special data recovery programs. But special services and laboratories can still recover deleted data.

How to delete a file (folder) in Windows was discussed in this article. I hope that now you can delete files or folders in different ways, including guaranteed deletion of files beyond recovery with special programs. However, if you run into any problems, do not hesitate to write in the comments. I will try to help.

How to delete all files in a folder except 5 files [closed]

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 7 years ago .

I would like to create a batch script in Windows to delete all files from a folder except for 5 specific files. For example, I would like to delete all *.txt files except for file1.txt, file2.txt, file3.txt, file4.txt and file5.txt.

6 Answers 6

I tried the following command and it works. The test1.txt and test2.txt is the file im excluding for deleting. What it does is define the exclusion file in / and loop through the txt files inside the folder. This is to ensure it targets only the .txt files.

The codes are adapted from this thread — How do I exclude specific file names from an MS DOS dir list?. I just modified a little bit to display the file extension(required for delete command) and the delete action.

This will work — don’t run it in any folder other than the one you intend:

Delete all *.txt files in folder SCANDIR except, file1.txt, file2.txt, file3.txt, file4.txt, file5.txt

This will create a new directory ( _emptyDir_ ) with no files in it. Then robocopy is used to purge the current directory of files not present in _emptyDir_ . As there are no files in the directory, all files get deleted except the files indicated to be excluded. Aditionaly, there is no need to rmdir _emptyDir_ , as robocopy will also handle it.

To use this method to only delete the txt files from directory, keeping the indicated files, the syntax will be

From empty directory to current directory, process only *.txt files, deleting all not existing in source (there is anything on it) except the excluded files.

In this case, as only txt files are processed, _emptyDir_ is not removed automatically.

Оцените статью