Back to Homepage
Tuesday 13 August 2024
9

How to Delete a Folder in CMD

Deleting folders via the Command Prompt (CMD) in Windows is a quick and efficient way to manage your file system, especially when dealing with multiple directories or automating tasks. CMD provides commands that allow you to remove both empty and non-empty folders with ease.

Deleting an Empty Folder with rmdir

The rmdir (Remove Directory) command is used to delete empty folders.

1. Basic Usage of rmdir:

To delete an empty folder, navigate to the directory containing the folder you want to delete or provide the full path:

rmdir "path\to\your\folder"

Example:

rmdir "C:\Users\YourUsername\Documents\OldFolder"

This command deletes the OldFolder directory if it is empty.

Deleting a Non-Empty Folder with rmdir /S

If the folder contains files or other subdirectories, you must use the /S flag to delete the folder and all its contents.

2. Using rmdir /S:

The /S flag is used to remove a directory along with all files and subdirectories it contains:

rmdir /S "path\to\your\folder"

Example:

rmdir /S "C:\Users\YourUsername\Documents\OldProject"

This command deletes the OldProject directory along with all files and folders inside it.

3. Confirming Deletion (Optional):

By default, rmdir /S will ask for confirmation before deleting the directory. If you want to skip the confirmation prompt, use the /Q (Quiet mode) flag:

rmdir /S /Q "path\to\your\folder"

Example:

rmdir /S /Q "C:\Users\YourUsername\Documents\OldProject"

This command deletes the OldProject directory and its contents without asking for confirmation.

Alternative Method: Using the del Command

The del command is primarily used to delete files, but it can also be used in conjunction with rmdir to ensure that all files within a directory are removed before deleting the directory itself.

4. Deleting All Files in a Folder:

First, delete all files in the folder, then remove the folder itself:

del /Q "path\to\your\folder\*.*"
   rmdir "path\to\your\folder"

Example:

del /Q "C:\Users\YourUsername\Documents\OldFolder\*.*"
   rmdir "C:\Users\YourUsername\Documents\OldFolder"

Examples of Deleting Folders in CMD

  • Delete an Empty Folder:
rmdir "C:\path\to\your\emptyFolder"
  • Delete a Folder with All Contents:
rmdir /S "C:\path\to\your\fullFolder"
  • Delete a Folder Silently Without Confirmation:
rmdir /S /Q "C:\path\to\your\quietFolder"

Official References

Share:
Created by:
Author photo

Jorge García

Fullstack developer