How to force delete all files in directory by cmd?

Force deleting files means permanently removing them without sending them to the recycle bin first. This can be useful when you want to completely erase files rather than just removing references to them. Force deleting bypasses the recycle bin, which is designed to allow recovery of deleted files if needed.

There are a few key reasons why you may want to force delete files rather than just deleting them normally:

  • To permanently erase sensitive files that you don’t want recoverable from the recycle bin.
  • To save disk space by not keeping copies of files in the recycle bin.
  • To remove corrupted or problematic files that won’t delete through normal means.
  • To clean out a directory and start fresh without leftover files.

Force deleting files using the command line gives you more control and power over permanently erasing files than using the graphical user interface. However, caution should be taken as force deleted files are not easily recoverable.

Prerequisites

In order to force delete files from the command line in Windows, your system must meet the following prerequisites:

You must be running a Windows operating system like Windows 10, Windows 8, Windows 7, etc. The steps outlined in this guide will only work on Windows and not other operating systems like Linux or MacOS. https://www.freecodecamp.org/news/how-to-force-delete-a-file-windows-command-prompt-cmd-delete/

You need to have administrative privileges on the computer in order to force delete files. Standard user accounts may not have permissions to force delete certain protected system files and folders. https://superuser.com/questions/204909/cant-delete-folder-and-i-am-admin-you-need-permission-to-perform-this-action

The files and folder you want to delete should not be currently opened by any program. Close any programs that may be accessing the files first before trying to delete.

You should backup important files in the folder before force deleting in case you need to recover them later.

Open Command Prompt

To open the command prompt on Windows, there are a few different methods you can use:

First, you can open the Start menu and search for “cmd” or “command prompt”. This will display the Command Prompt app that you can click on to launch.

Alternatively, you can press Windows key + R to open the Run dialog box. Then type “cmd” and hit enter to launch the command prompt window.

You can also access Command Prompt from the Start menu itself. Click on the Windows logo Start button, expand the “Windows System” folder, and select “Command Prompt” to launch it.

Finally, you can right-click on the Start button or press Windows key + X to bring up a context menu, and select “Command Prompt” or “Windows Terminal” from the menu.

The easiest and fastest way is to use the keyboard shortcut Windows key + R, type cmd, and press Enter. This will immediately open the command prompt ready for you to enter commands.

Navigate to Directory

To navigate to the directory you want to delete files from, use the cd (change directory) command in Command Prompt. The cd command allows you to move between directories and specify the path of the folder you want to operate in.

The basic syntax is:

cd [folderpath]

For example, to change to the C:\Users\Name\Documents directory, you would type:

cd C:\Users\Name\Documents

You can also navigate up one directory level by typing cd.. and return to the root directory by just using cd without any arguments. The cd command is essential for being able to access the folder you want before deleting files. For more details, refer to the Microsoft guide on cd and the Wikipedia page on the cd command.

View Directory Contents

To view the contents of a directory in Command Prompt, you can use the ‘dir’ command. The dir command will list all files and subdirectories within the specified directory (Microsoft).

For example, to view the contents of the current directory you are in, simply type ‘dir’ and hit enter. To view the contents of a different directory, you can provide the directory path after dir. For example: dir C:\Users\Name\Documents will show the contents of the Documents folder for the user Name (Lifewire).

The dir command has many useful options and switches to control the output. You can use /s to see contents of all subdirectories recursively, /b for bare format, /o to sort by columns, /ad to see only directories, and more. See GeeksforGeeks for details on dir options.

Force Delete Files

To force delete all files including read-only files in a directory in Command Prompt, you can use the ‘del’ command with the ‘/f’ and ‘/s’ switches.

The ‘/f’ switch will force the deletion of read-only files, while the ‘/s’ switch will delete the specified files in the current directory and all subdirectories.

The syntax is:

del /f /s *.*

This will delete all files with any file extension in the current folder and all subfolders.

For example:

del /f /s *.*

The above command will force delete everything in the current directory and subdirectories without prompting for confirmation. Be very careful when running this command as it cannot be undone.

According to Microsoft’s documentation, “The /F command-line option forces deletion of read-only files. /S Deletes specified files from the current directory and all subdirectories. Displays the names of the files as it deletes them.” [1]

Confirm Deletion

After deleting the files, it’s recommended to double check and confirm that they have been permanently removed. Run the dir command again in the same directory to view the updated contents. This will verify that the files are no longer listed. Seeing an empty folder confirms that del *.* successfully removed everything.

As an extra precaution, check the Recycle Bin to ensure the deleted files have not been moved there. If the files were moved to the Recycle Bin rather than permanently deleted, they can still be recovered. However, if the Recycle Bin is empty, it guarantees the files were completely wiped from the system.

According to Microsoft’s forum, right-clicking the Recycle Bin icon and going to Properties > General tab allows users to enable a delete confirmation dialog in Windows 10 and 11 [1]. This adds an extra prompt before permanently deleting files.

Delete Folder

To delete an entire folder and all its contents using the Command Prompt, you can use the rd (rmdir) command with the /s and /q switches.

The /s switch will delete the specified folder and all subfolders and files within it. The /q switch will suppress any prompts for confirmation before deleting folders.

For example, to delete a folder called “foldername” along with everything inside it, you would type:

rd /s /q foldername

This will immediately and recursively delete foldername and everything within it without asking for confirmation. Be very careful when using the /s and /q switches together, as you can accidentally delete important folders and data.

To delete empty folders without recursion, you can simply use:

rd foldername

This will only remove empty folders. If the folder contains files or subfolders, you will get an error message.

Alternatives

While permanently deleting files with the Command Prompt is effective, there are other methods you can use as well. An alternative on Windows is to use PowerShell [1].

PowerShell includes the Remove-Item cmdlet which allows you to delete files and directories. To permanently delete files, you can use the -Force parameter along with -Recurse to delete all files and subfolders:

Remove-Item -Path C:\MyFolder -Recurse -Force

This will force delete all files and folders under C:\MyFolder without prompting for confirmation. The files will be deleted permanently without placing them in the Recycle Bin.

Recover Deleted Files

If you accidentally delete files from a directory in Command Prompt, there are still ways to try and recover those files. However, recovery becomes less likely the more your computer writes new data after the deletion. So act quickly if you need to restore lost files.

One of the easiest ways to recover deleted files on Windows is to use the Previous Versions feature [1]. You can right-click on the parent folder, select “Properties”, and go to the Previous Versions tab. This allows you to restore a snapshot of the folder from before the files were deleted.

There are also third party tools designed for recovering deleted files, such as Recuva [2] and PhotoRec. These scan your drives and attempt to recover deleted data. However, they don’t guarantee recovery, especially if too much new data was written after deletion.

Recovering deleted files often takes specialist tools and skills. So if the files were particularly important, it may be worth consulting a data recovery service. But the above methods can help recover deleted files in many cases.