Diskpart Has Encountered an Error: The Media Is Write Protected

February 14, 2019

In this article, you will get causes for Diskpart has encountered an error the media is write protected and fix the Diskpart clean error accordingly.

About this error

When you use Diskpart clean or clean all command on USB drive, you may receive the following error message.

Diskpart has encountered an error the media is write protected

See the system event log for more information

Diskpart Has Encountered an Error the Media Is Write Protected

Besides, this error message will occur when you use the command above on SD card, HDD, SSD, SanDisk drive and more.


On the market, there is some mass storage device provides a write-protection function to protect files and folders. However, sometimes, you still cannot access files and folders. Besides, some drive does not have a write-protection switch and still can be set to read-only mode.

In the following, I’ll list some possible causes.

  • Your storage media is damaged by virus or malware.

  • You accidentally set the read-only switch to ON side and the media becomes un-writable.

  • You set the disk is to read-only in Property or Registry.

  • Your drive has physical damages, such as bad sectors.

Diskpart has encountered an error the media is write protected – fixed

To fix this error and ensure the security of your data, you’d better to backup files first and then try the following solutions one by one. Some of the following operations will lead to data loss.

Change the value of Write-protect key in Registry

Step 1. Type regedit in the Run window.

Step 2. Navigate to the following sub-key and find the WriteProtect key on the right panel.


Step 3. If there is no StorageDevicePolicies key, you need to create one. Right-click on Control, select New and Key option in order and then name it as StorageDevicePolicies.

Create New Key

Step 4.  Right-click StorageDevicePolicies, select New and DWORD (32-bit) Value and then name it as WriteProtect.

Step 5.  Right-click the newly created WriteProtect key and change the Value to 0. And then click OK to exit this window.

Step 6. Check if you can access your storage device and data.

Type attributes disk clear readonly command first

When you use Diskpart clean command, you will receive Diskpart has encountered an error the media is write protected on USB/SD card/HDD/SSD if you directly type clean or clean all command instead of typing attributes disk clear readonly command first.

Step 1. Type cmd in the search box, right-click it and select Run as administrator.

Step 2. Type diskpart in the pop-up window and press Enter.

Step 3. Type the commands below in order and hit Enter to run it.

  • list disk
  • select disk n (n refers to the number of device you want to clean)
  • attributes disk clear readonly
  • clean 

Attributes Disk Clear Readonly First

Step 4. Exit this window and check if it works.

Repair the corrupted file system

If your drive has corrupted file system, you can use Diskpart to clear all write-protection file attributions from the disk and reformat it again.

Step 1.Open cmd and type diskpart in the window.

Step 2. Type the commands below and press Enter.

  • list disk
  • select disk m (m refers to the drive containing corrupted file system)
  • attributes disk clear readonly
  • clean 
  • create partition primary
  • format fs=fat32 (or format fs=ntfs)

Repair Corrupted File System

Step 3. Exit this window and then see if you can access your device and data.

Change Read-only mode

If the device is in read-only mode, you need to check it first, and then change the Current Read-only State from Yes to No.

Step 1. Open cmd window and type diskpart in the command prompt.

Step 2. Type the following commands and hit Enter.

  • list disk
  • select disk r ( r refers to the disk you want to clean)
  • attributes disk

Attributes Disk

This command tells you if your device is in read-only mode. If yes, see Step 3. If not, try to format the media for reuse.

Step 3. Type attributes disk clear readonly command in the command prompt and hit Enter.


1. If your disk is locked by an active process, you still need to reboot the computer.

2. If your device is USB drive, you still need to check the write-protect switch.

Step 4. Try the clean command again and see if this error occurs again.

Format the media for reuse

Formatting partition is the last resort to write-protected issues. It will lead to data loss if you do not have a file backup.

To fix the Diskpart clean error: Diskpart has encountered an error the media is write protected, you can use the write-protected removal tool AOMEI Partition Assistant. It can format USB drive/SD card/ HDD/SSD with or without Write Protected in a very short time. And all the steps are very easy to learn, even for a beginner. You just need to follow the steps below and move your finger.

Step 1. Download the write-protected removal tool – free AOMEI Partition Assistant and run it. Right click the write-protected media in the main page, and then select Format Partition.

Format Partition

Step 2. Select NTFS or FAT32 file system from the drop-down menu and click OK to exit.

Partition Label

Step 3. Click Apply and then Proceed to start the pending operation.


Besides above operation, AOMEI Partition Assistant can help you do more things and fix some problems. You can use it to move partition to end of the disk, clone Windows to USB drive, etc. Also, you can use it to fix virtual disk service error the volume size is too big, extend volume greyed out, Diskpart has encountered an error access is denied, etc.

AOMEI Partition Assistant, the free version of partition manager, can satisfy all your basic needs. But if you want to fix the problem free version cannot or just get more features, you can upgrade to AOMEI Partition Assistant Professional or AOMEI Partition Assistant Server according to your needs and the operating system you are using now.

With the Professional edition, you can convert GPT to MBR without data loss using command prompt, allocate free space, fix Convert to Basic Disk greyed out, and more.

Wrapping things up

To fix Diskpart has encountered an error the media is write protected, you can try to change the value of the Writeprotect key in Registry, type attributes disk clear readonly first and then clean, repair corrupted file system of the media, change the read-only mode from Yes to No.

If none of the above methods works for you, you can try the last one – formatting partition with a write-protected removal tool AOMEI Partition Assistant. With it, all things become easy. You do not need to learn the complicated commands, which will lead to data loss if one of the process goes wrong. In this software, all steps are fixed and there is little risk to make a mistake.