How to remove windows file

Script examples to learn how to delete Windows files using the cmd prompt and the dir command.

To delete Windows files using a cmd script, it is recommended to use the del command. The del or delete command provides many very useful options to be in control of the deletion process. The Command Prompt is a text-based user interface that enables users to execute commands and perform scripted tasks with their computer.

It’s a powerful tool that can be used in a variety of ways. Users can use the Windows command prompt to recursively copy files, recursively delete files, manage the registry and much more.

1. Benefits of deleting files using the command prompt

Deleting files using the command prompt can provide several benefits over other methods. Firstly, it allows for faster deletion of multiple files or directories with a single command, reducing the time needed to perform the task. Additionally, the command prompt can be used to delete files that are otherwise difficult to delete using other methods.

For example, files with long file names, special characters or those that are locked by other applications can be deleted using the command prompt. Finally, using the command prompt can be more efficient for advanced users who are familiar with command line interfaces and prefer to use them over graphical user interfaces.

In this article, we will show you how to delete files with the Windows command prompt and why you should start using it. It’s also an easy way to execute commands without using the graphical user interface (GUI). The cmd is a part of the administration commands available in MS systems. To delete a file with cmd, you need to provide the exact path of the file.

If you don’t know where the file is located, you can use the dir command to locate it. The main option that we suggest using is the deletion confirmation, which allows you to control one last time which file to delete. However, please note that the files deleted with the del command line are not stored in the Recycle Bin, so you cannot restore them.

Make sure you delete the proper file. The delete operation is irreversible.

2. Command line script to delete files the simple way

Deleting files through the command prompt can be done in a variety of ways, with varying levels of confirmation prompts and complexity. One simple way to delete a file through the command prompt is to use the “del” command with the explicit file path. This command will delete the file without any confirmation prompts, so it’s important to double-check the file path before executing the command.

Here is an example of how to use this command to delete a file with the path “C:\Folder\file (1).txt”.

del "C:\Folder\file (1).txt"

Keep in mind that this command will permanently delete the file without moving it to the Recycle Bin, so use it with caution.

3. Delete files with a confirmation prompt

When deleting files with the command prompt, it’s important to exercise caution and double-check the filename before proceeding with the deletion. One way to ensure this is by using the confirmation prompt option. To delete a file with confirmation, you can use the “/p” option followed by the file path. For example, to delete a file named “file (2).txt” located in the “C:\Folder” directory, you can use the command “del /p “C:\Folder\file (2).txt””.

This will prompt a confirmation message asking if you want to delete the specified file. If you confirm the deletion by pressing “Y”, the file will be permanently deleted. Useful for example to delete a file and make sure to double check the filename.

del /p "C:\Folder\file (2).txt"

Then a prompt is displayed:
C:\Folder\file (2).txt, Delete (Y/N)?

Image with screenshot how to delete a file in windows cmd.

Screenshot of Windows command prompt of file deletion a file in cmd.

4. Use the delete command without confirmation

The other way to delete Windows files with cmd is to disable the confirmation for a large number of files for example. Indeed, if you want to delete multiple files without being prompted for confirmation at all, you can use the delete command in quiet mode.

This mode disables the confirmation prompt and deletes files directly. To use this mode, you can type “del /q” followed by the path and name of the file you want to delete. For example, to delete a file named “file (3).txt” located in the “C:\Folder” directory, you can use the following command.

del /q "C:\Folder\file (3).txt"

5. Delete read only files in command line with force option

When trying to delete a read-only file, the command prompt may display an error message that says:

Access is denied.

This is because the file is set to read-only. To bypass the read-only attribute and force the deletion of the file, you can use the del command with the /f option. For example, to delete a read-only file located in “C:\Folder” named “file (3).txt”, you can use the following command: “del /f C:\Folder\file (3).txt”. This will delete the file without any error messages or prompts.

So, to force deletion of a read only file, and bypass the read-only attribute, use the del /f option, /f stands for force.

del /f "C:\Folder\file (3).txt"

Conclusion on Windows files deletion and its options

To conclude, the “del /p” option is definitely the best cmd option to make sure to delete the proper files from the disk. Use it when only when deleting specific and targeted files. However, to delete a large number of files, use the quiet and the force options.

To delete Windows files using cmd, the command prompt definitely provide several benefits over other methods, i.e. mainly the Windows GUI. It allows for faster deletion of multiple files or directories with a single command, reducing the time needed to perform the task. Additionally, the command prompt can be used to delete files that are otherwise difficult to delete using other methods. The command prompt is a text-based user interface that enables users to execute commands and perform scripted tasks with their computer.

Finally, using the command prompt can be much more efficient for advanced users who are familiar with command line interfaces and prefer to use them over graphical user interfaces. Check out how to copy a file to another folder in command line.

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

  1. Delete file to “Recycle Bin”
  2. Permanent delete
  3. Guaranteed file deletion
  4. Delete via Command promt
  5. Delete via PowerShell
  6. Comparison table

Article in other languages:
?? – Cómo eliminar un archivo en Windows (varias formas)
?? – Как удалить файл в Windows (несколько способов)

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.

How to delete a file in windows

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.

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.

Recycle Bin 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.

Recycle Bin restore files and folders

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

Empty Recycle Bin

? This is a permanent data deletion!

How to permanent Delete

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

how to permanently delete file (folder) in windows

Permanently delete folder

? This is a permanent data deletion!

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)

Microsoft support

? 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.
cipher /W:X

/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.

Guaranteed file deletion - cipher utility

Utility Cipher.exe

? Using the CIPHER utility – this is guaranteed data deletion!

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.
format X: /P:count

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.

Guaranteed file deletion format

Utility Format.com

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

? Using the FORMAT utility – this is guaranteed data deletion!

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.
del mydoc.txt

Delete files via command prompt

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.

? This is a permanent data deletion!

How to delete a folder via command prompt

  1. Run the command prompt;
  2. Go to the desired folder;
  3. Run the RD command.
rd "folder name" /S

Delete folders via command prompt

/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.

? This is a permanent data deletion!

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.

 Remove-Item "X" -Recurse

PowerShell delete files and folders

Windows PowerShell

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

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

? This is a permanent data deletion!

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.

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

on August 5, 2015

Deleting files is one of the frequently done operation from Windows command prompt. This post explains how to use ‘del’ command from CMD for different use cases like deleting a single file, deleting files in bulk using wild cards etc. Before we start to look at the syntax, note that the command works only for files and can’t handle folders.

How to delete a file

Run del command with the name of the file to be deleted, you are done!

del filename

You do not see message after running the command if the file is deleted successfully. Error message is shown only when something goes wrong.

Delete files in bulk

Del command recognizes wildcard(*) and so can be used to delete files in bulk from CMD.  Some examples below.
To delete all the files in current folder

del *

To delete all the files with ‘log’ extension

del *.log

Delete all files having the prefix ‘abc’

del abc*

Delete all files having ‘PIC’ somewhere in the file name.

del *PIC*

The above are the basic use cases of del command. Continue to read below for non trivial use cases.

Delete multiple files

‘Del’ command can accept multiple files as argument

del filename1 filename2 filename3 filename4....

Example:

D:\>dir /s /b
1.pdf 2.pdf 3.pdf
D:\>del 1.pdf 2.pdf 3.pdf
D:\>
D:\>dir /s /b
D:\>

Delete Read only files

We can’t delete a read-only file using simple‘del’ command. We get access denied error in this scenario.

c:\>attrib readonlyfile.txt
A    R       C:\readonlyfile.txt
c:\>del readonlyfile.txt
c:\readonlyfile.txt Access is denied.
c:\>

A read-only file can be deleted by adding /F flag.

del /F readonlyfile.txt

Alternatively, we can use the below command too

del /A:R readonlyfile.txt

You probably want to remove some confidential files from your computer that should never fall into someone else’s hands. So, you move forward to delete them. But do you know that just deleting from Windows won’t permanently remove any files?

Yes, deleted files are recoverable until you clean the Recycle bin, which is like the graveyard of deleted data. Except, this graveyard acts as a reincarnation center for such data. Anyone who can access your computer will be able to restore the data you thought was gone forever.

In this article, we will be talking about the whole deleting process, the methods you should use to permanently delete files, and some advanced removal techniques that will make sure your data becomes irrecoverable from even forensic recovery experts.

Table of Contents

How to Permanently Delete Files?

After deleting files, we move toward the desktop, right-click on the recycle bin and choose Empty Recycle Bin to empty it. But some other methods will help you directly empty the recycle bin. Such methods include:

Set Recycle Bin Settings to Not Hold Deleted Data

The manual method to empty Recycle Bin becomes tedious when you have to delete files periodically. So, if you don’t want to go through the extra step of emptying Recycle bin each time, you can probably try this setting:

  1. Right-click on the Recycle bin and choose Properties.
    Recycle-bin-properties
  2. Select the folder you want to implement this setting on.
    Select Drive
  3. Under Settings for selected location, choose the radio button with ‘Don’t move files to the Recycle Bin. Remove files immediately when deleted.
    Permanent Deletion
  4. Hit on Apply, and then OK.

Now, when you will delete any file and agree with the confirmation dialog box, it won’t reside in Recycle Bin.

Use Keyboard Shortcut

The fastest and easiest method to delete any file without making it recoverable from the Recycle bin is to use a key combination. Following are the steps you will need to follow to do so:

  1. Select the file or folder you want to delete.
  2. Press the Shift + Del keys.
  3. Hit the Yes button.

Using Command Prompt

Command Prompt is featured with various inbuilt commands to imitate and sometimes surpass the GUI(Graphical User Interface) methods of accomplishing tasks. For deleting purposes, the del command comes in handy. The file deleted using this command will bypass the recycle bin storage.

You can follow the steps below to use the del command:

  1. Press Windows + R keys to open Run.
  2. Type cmd, and hit Ctrl + Shift + Enter to open the command prompt with admin access.
    cmd-from-run
  3. Click on the Yes button on the UAC prompt.
  4. Now run the command: del <file path>
    del-command

Change the ‘<file path>’ with the full path name, for instance: C:\Users\acer\Downloads\test.txt. In case you don’t know the file path, you can just right-click on the file and choose Copy as path. Then, paste it in place of ‘<file path>‘ and remove the quotes.

Using Powershell

Equivalent to the del command in Command Prompt is the Remove-Item of Powershell. But being like an upgraded version of command prompt, Powershell offers more powerful and flexible options to accomplish the job. These steps will help you to delete files using Powershell:

  1. Open the Run prompt.
  2. Type powershell, and hit Ctrl + Shift + Enter to open an elevated Powershell.
    powershell-from-run
  3. Hit the Yes button on the UAC prompt.
  4. Execute Remove-Item -Path <file path>
    remove-item

Yet again, change the ‘<file path>‘ with the actual file path as guided in the above section.

Can Deleted files be Recovered?

As mentioned earlier, deleting merely means making files inaccessible or removing their pointers. Emptying the recycle bin makes the data unrecoverable but from only Windows. The blocks of disks will hold data forever until they are emptied or overwritten, or the disk itself gets damaged. 

Utilizing the actual data written over blocks, many recovery software are able to reverse engineer the deleted files. Thus, deleted files are recoverable. 

So, how to permanently delete them? Well, the best method would be to overwrite data over the storage blocks. But manually finding those specific blocks isn’t feasible at all. 

There are some specialized utility tools to permanently delete data from your disk. File shredders work by overwriting data on the storage blocks with random bits of information. In that way, even if any recovery software scans over the data holding block, no more than non-sensical data is found. 

Thus, it’s always better to utilize a specialized eraser or shredder software to completely dump a file without any leftovers.

However, modern SSDs can respond to the Trim command from the motherboard. The command can help SSDs to identify the unallocated blocks and clean them. But not all PCs support trim, and HDDs are completely deprived of this privilege. 

Moreover, trim runs in the background and not right after deletion, so immediately deleted data would still be recoverable. 

Use Cipher and Sdelete to Permanently Delete Files

For the permanent deletion job, two command-line tools are provided by Microsoft Sysinternals, Sdelete.exe and Cipher.exe. You can use any one of the tools. 

Although both help in achieving the same end result of deleting files by overwriting the storage blocks/clusters, Sdelete needs to be downloaded. In contrast, Cipher has been an inbuilt command line since Windows Vista and further. 

Cipher

Cipher’s job is basically to encrypt/decrypt data while storing. Its switch /w was created to delete the backup data of the encrypted files for Windows servers. Later it was introduced as a command line for client Windows as well. 

The command ensures permanent data removal by overwriting the occupied blocks thrice with random bits. Thus, it will take up a lot of time and resources when executed. 

You will have to first delete the file. Then, To overwrite the blocks, you can follow the steps below:

  1. Press Windows + R to initiate Run.
  2. Type cmd, and hit Ctrl + Shift + Enter to open the command prompt in admin mode.
  3. Go with the Yes button on the UAC prompt.
  4. Now run the command: cipher /w: c

Change ‘c’ with the drive letter where the deleted file used to reside.

Sdelete

Sdelete works differently than the cipher. It overwrites particular blocks while deleting the file, not overall unallocated space. Thus, it is faster and less resource-consuming.

To use Sdelete, you will have to download its zip file from the website. Then, follow the given steps:

  1. Go to the downloaded location and right-click on the zip file.
  2. Choose Extract All…
    Sdelete-Extract-all
  3. Click on Extract to extract it to the current location on your disk.
  4. Now copy the path of the extracted folder. 

The next step would be setting it as an environment variable so that it can be executed directly from the command prompt without needing to specify the sdelete.exe object each time. 

  1. Press Windows + R to open Run.
  2. Type sysdm.cpl and hit Enter to open System Properties.
  3. Move to the Advanced tab and click on Environment Variables…
    advanced-environment-variable
  4. Select Path and hit Edit…
    sdelete-path-edit
  5. Click on the New button and press Ctrl+V to paste the sdelete.exe folder path.
    add-new-variable
  6. Remove quotes and hit OK.
  7. Now, open Run, type cmd, and press Ctrl + Shift + Enter.
  8. Click on the Yes button.
  9. Run the command: sdelete -s <file path>
    sdelete-command
    Replace ‘<file path>’ with the actual one.

Where Does the Deleted Data Go?

Data in a disk is stored in its blocks, which are like small container packets. An individual block has a certain amount of storage capacity in bits/bytes. And piles of blocks add up to make sufficient capacity within any disk. 

To read/write any data from numerous blocks, Windows needs to have accurate data pointers locating specific blocks. When a file is deleted, Windows only removes information about its location(pointers) but not the actual data from the blocks.

Deleted file data can be retrieved if its block location is known. So, to let users undo any accidental deletion of files, Windows keeps a copy of location data(pointers) along with its name, creation date, and others(called metadata as a whole) in the recycle bin.

A recycle bin is a combination of hidden folders taking up a certain percentage of each drive. If it fills up extravagantly, the metadata of the earliest files is removed. However, the recycle bin should be emptied to make any recently deleted file unrecoverable from within the Windows. 

  • How to make windows 10 faster
  • How to run windows on mac
  • How to remove windows defender completely
  • How to setup environment variable windows
  • How to make usb bootable windows 10 usb