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
This error message will occur when using the command on various storage devices, including SD cards, HDDs, SSDs, SanDisk drives, and more.
On the market, there are mass storage devices that provide a write-protection function to safeguard files and folders. However, despite this feature, issues may still arise, preventing access to files and folders. Furthermore, some drives lack a physical write-protection switch but can still be set to read-only mode. This can occur due to various reasons, which will be explored in the following discussion.
▶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.
To resolve the error, it's recommended to back up your files first, as some of the following solutions may result in data loss. After backing up your data, you can try the solutions one by one, starting with the least invasive ones. This will help prevent any potential data loss and ensure the security of your files.
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, where you can toggle the write protection on or off.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies
Step 3. If there is no StorageDevicePolicies key, you need to create one by right-clicking on "Control", selecting "New" and then "Key", then naming it as "StorageDevicePolicies."
Step 4. To disable write protection on a storage device, create a new DWORD value in the StorageDevicePolicies registry key and name it "WriteProtect".
Step 5. Right-click the newly created WriteProtect key and change the Value to 0, then click OK to exit the window.
Step 6. Check if you can access your storage device and data.
When using Diskpart's clean command on a USB/SD card/HDD/SSD, but getting an error message saying "Diskpart has encountered an error: The media is write protected", you can first type "attributes disk clear readonly" before running the clean or clean all command. This can help resolve the issue.
Step 1. To open the Command Prompt as an administrator, start by typing "cmd" into the search box. Once the Command Prompt appears in the search results, right-click on it and select the "Run as administrator" option. This will open the Command Prompt with elevated privileges, allowing you to run commands and perform tasks that require administrative access.
Step 2. Type diskpart in the pop-up window and press Enter.
Step 3. To install the required libraries, run the following commands in order and hit Enter to execute each one: `pip install pandas`, `pip install scipy`, `pip install matplotlib`, `pip install scikit-learn`, `pip install numpy`, `pip install tensorflow`, `pip install keras`, `pip install pandas-datareader`, `pip install yfinance`, `pip install seaborn`, `pip install plotly`, `pip install statsmodels`, `pip install catboost`, `pip install lightgbm`, and `pip install xgboost`.
▶list disk
▶select disk n To clean your devices, start by turning them off and unplugging them from the power source. Next, use a soft cloth to wipe down the exterior, paying special attention to any areas with visible dust or dirt buildup.
▶attributes disk clear readonly
▶clean
Step 4. Exit this window and check if it works.
If your drive has a corrupted file system, you can use Diskpart to remove all write-protection file attributions from the disk and then reformat it. This can help resolve the issue.
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)
Step 3. Exit this window and then see if you can access your device and data.
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)
▶attribute disk
This command checks if your device is in read-only mode. If it is, you'll need to see Step 3. If not, you'll need to format the media for reuse.
Step 3. To clear the readonly attribute from a disk, open the Command Prompt and type the command `attributes disk clear readonly` and press Enter. This will remove the readonly attribute from the disk, allowing you to make changes to the disk and its contents.
Step 4. Try the clean command again and see if this error occurs again.
Formatting a partition is the last resort to resolve write-protected issues, but it comes with the risk of data loss if you don't have a file backup. To fix the Diskpart clean error: "Diskpart has encountered an error, the media is write-protected," you can use a free partition manager like Qiling Disk Master Standard. This software can format write-protected USB drives, SD cards, HDDs, or SSDs due to corrupted file systems, and the steps are easy to follow, even for beginners.
Step 1. Download the free Qiling Disk Master tool, run it, and right-click the write-protected media in the main page, then select Format Partition.
Step 2. Select NTFS or FAT32 file system from the drop-down menu.
Step 3. Click Proceed to start the pending operation.
Qiling Disk Master allows you to perform various operations beyond just resizing partitions, including moving partitions to the end of the disk, cloning Windows to a USB drive, and more. It can also assist in resolving issues such as virtual disk service errors caused by large volume sizes, greyed-out volume extension options, and Diskpart errors due to access being denied.
Qiling Disk Master, the free version of partition manager, meets basic needs, but for more features and problem-solving capabilities, upgrade to Qiling Disk Master Professional or Server. The advanced versions offer additional features such as converting MBR to GPT without deleting partitions, allocating free space, and converting dynamic disks to basic ones without data loss.
To resolve the "Diskpart has encountered an error the media is write-protected" issue, you can try modifying the Writeprotect key in the Registry, clearing the readonly attribute and then running a disk clean and repair operation to fix the corrupted file system on the media, and finally change the read-only mode from Yes to No.
If none of the above methods work for you, you can try formatting the partition with Qiling Disk Master. This software makes the process easy and safe, eliminating the risk of data loss that comes with manual formatting commands. With Qiling Disk Master, all steps are predetermined and straightforward, reducing the likelihood of making a mistake.