How to Erase a Disk from Command Line in Mac OS X

Aug 30, 2016 - 53 Comments

Erase disk from command line of Mac OS

Some Mac users may require the ability to erase a disk or erase a hard drive from the command line on Mac OS, a task which is typically performed through the Disk Utility application from the GUI. The command line approach to disk erasure in macOS is a bit different and it requires precise syntax to insure that you are erasing the proper disk, making this method of erasing any disk only appropriate for advanced Mac users.

This guide will walk through how to erase and format an entire target disk using exclusively the command line on any Mac using macOS or Mac OS X. You can choose any common file system format the disk to after it has been erased, including ExFAT, FAT32, HFS+, or JHFS+.


Notice that this is aiming to erase the entire disk from the command line here, this is not just erasing a volume or partition on the target disk. The entire target disk is erased, all data on the target disk is destroyed using this approach, with no volumes or partitions or any data remaining. Do not misunderstand that, otherwise you will inevitably permanently lose data when it is erased and destroyed. Remember the command line is unforgiving, if you are not comfortable at the command line it would be much more appropriate to erase and format a disk using Disk Utility in the standard interface of Mac OS X.

How to Erase a Disk from the Command Line of Mac OS

To get started, you’ll need to launch the Terminal application on the Mac which gives access to the command line. It can be found with Spotlight, Launchpad, or in the /Applications/Utilities/ folder.

To erase a disk from the command line on the Mac, we’ll use the familiar “diskutil” command with the eraseDisk verb and other appropriate flags to specify options for how we want to erase the disk, and to identify which disk is to be erased.

The basic syntax for erasing a disk from the command line in macOS is as follows:

diskutil eraseDisk FILE_SYSTEM DISK_NAME DISK_IDENTIFIER

For example, let’s say you have used “diskutil list” to show all of mounted drives on a Mac from the command line, and you have determined the appropriate drive to erase is identified as /dev/disk6s2, you want the disk name to be “Emptied” and you want the new disk file system format type to be Mac OS Extended Journaled (JHFS+), the syntax would be the following:

diskutil eraseDisk JHFS+ Emptied /dev/disk6s2

It is absolutely critical that you use proper syntax when identifying the disk to erase. Improper identification may lead to erasing the wrong disk, permanently destroying any data on it. Do not screw this up. If you are unsure, you can find the disk ID node with “diskutil info “DISK NAME” |grep Device”.

For some quick reference, here are a few examples of various disk erasure methods for different file system format types. As always, be sure you change the disk node as appropriate for your disk.

Formatting a Disk to Mac OS Extended Journaled (JHFS+) from Terminal in Mac OS X

diskutil eraseDisk JHFS+ DiskName /dev/DiskNodeID

Formatting a Disk to Mac OS Extended (HFS+) from Terminal in Mac OS X

diskutil eraseDisk HFS+ DiskName /dev/DiskNodeID

Formatting a Disk to MS-DOS fat32 from the Command Line in Mac OS X

diskutil eraseDisk FAT32 DiskNameGoesHere /dev/DiskNodeIDHere

Formatting a Disk to ExFAT from the Command Line in Mac OS X

diskutil eraseDisk ExFAT DiskName /dev/DiskNodeID

Again, any of these commands erase the entire target disk and obliterates any data on it.

Users who would like additional details or information about the other options available including MBR and GPT settings can query the man page with “man diskutil” and searching for “eraseDisk”, or execute the command with no specifics like so:

diskutil eraseDisk
Usage: diskutil eraseDisk format name [APM[Format]|MBR[Format]|GPT[Format]]
MountPoint|DiskIdentifier|DeviceNode
Completely erase an existing whole disk. All volumes on this disk will be
destroyed. Ownership of the affected disk is required.
Format is the specific file system name you want to erase it as (HFS+, etc.).
Name is the (new) volume name (subject to file system naming restrictions),
or can be specified as %noformat% to skip initialization (newfs).
You cannot erase the boot disk.
Example: diskutil eraseDisk JHFS+ UntitledUFS disk3

Finally, it’s worth mentioning that if you want to erase the currently booted disk from the command line from this method, you’d want to do so from a boot disk or from recovery mode. Single User Mode alone is not sufficient to erase the actively booted operating system.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

53 Comments

» Comments RSS Feed

  1. faith kemunto says:

    msdos should be fat32that true .thanks

  2. antony says:

    thanks for the feedback

  3. Stephen says:

    Hi there,
    I ran the following: diskutil eraseDisk JHFS+ Heroines /dev/disk0
    and the following dialogue occurred after the command was given and the disk proceeded to start erasing.
    Started erase on disk0
    Unmounting disk
    Error: -69888: Couldn’t unmount disk

    Disk Utility had already failed to unmount disk so I tried your method getting this result. Q. Is there anything I can do to reformat the drive?

    • Johnny says:

      You might try to boot into recovery holding down Command + R. Once there look at the top of the screen where the file menu is & select Tools or Utilities. In the drop-down menu select Terminal, this will allow the disk to be dismounted.

  4. Benny says:

    Incredibly useful instructions. Thanks for the assistance.

  5. themacmeister says:

    This saved my bacon… said partitions could not be resized/removed in Disk Utility… used the command above (for ExFAT entire disk) and it worked perfectly.

    • Paul says:

      I tried with exFat as well but I still can’t partition:

      diskutil erasedisk exFat 4TBWD GPT /dev/disk4;

      The only way I can partition is if I run it with hfs+:

      diskutil erasedisk hfs+ 4TBWD GPT /dev/disk4;

      Any way to add a number of partitions to the exFat erase command?

  6. Brandon says:

    About how long should erasing the disk take?

  7. Trevor Smith says:

    Hello can you give the correct command line command to format a 16gb stick (/dev/disk2) into a Fat32 with MBR name can remain untitled.

    thanks

  8. Paul says:

    I’ve tried using the eraseDisk with and without “sudo” command but it always tells me disk2s2 could not mount after erase.
    Error -69832 keeps showing.

    Any help?

  9. Paul says:

    I’ve tried to erase my 500GB HDD using:

    (sudo) diskutil eraseDisk JHFS+ Name /Dev/disk2

    But I keep getting:
    Could not mount disk2s2 after erase
    error -69832: file system formatter failed

    Can anyone help me out?

  10. BJ says:

    Thank you! This post is older, but incredibly relevant when you need it. I wish I’d found it before I wasted hours trying to figure it out another way. Again, thank you. The most helpful thing I’ve seen online in days.

  11. JG says:

    What Terminal-App is shown on top of the Page?

  12. jn says:

    very useful instructions, esp when disk utility is unable to format your disk

  13. Luis says:

    I’m trying to format an external ssd 256GB hd that used to be the main disk in a Windows machine corrupted by a Virus.
    I follow the instructions, and I received the message:

    “Error: -69760: Unable to write to the last block of the device”

    Any idea?

    Thanks in advance.

    • Gopper says:

      This is an article for Mac, it is erasing a hard drive from a Mac command line, it is not a Windows tutorial.

      You can, however, format a Windows hard drive from a Mac if you connect the Windows hard drive to the Mac and format it through Disk Utility, or the command line as described above. If you are having a hard time using the command line, try the Disk Utility app, which is simpler and uses the GUI.

      • Luis says:

        Gopper,
        Who said that I was using the command line code in a windows machine? In fact, that commands wont work in windows.

        Someone have a better/more pro-positive and useful answer?
        Thanks,

        Luis

    • Monk_ says:

      Hey Luis, have you found a solution to your problem?
      I’ve been experiencing the same for the past two months, I’m about to lose it.

      • Luis says:

        Hi Monk,
        I’m afraid I didn’t find a solution. I bought a new hd, and keeping the old one until find a way to format it. How are you going in your searching?

    • JB says:

      Hi Luis,

      having same issue with an Iomega 1.5TB external drive. Did you ever find a solution ?

      regards

  14. Danny says:

    Thank you, this helped me incredibly and made the rest of my job easier.

  15. justinQ says:

    Thank you for this.
    My disk utility can’t read my ubuntu USB installer so I needed to format it using the terminal.

  16. Sha says:

    Hi,
    I tried a lot of command from here but was not able to repair my 16GB flash drive. I got this error:
    Error: -69825: Wiping volume data to prevent future accidental probing failed
    I spent two days on it and found no solution. Please help me on this?
    Thanks

  17. Jaxom says:

    Hey, thanks for the useful tip !

    On MacOS 10.11.6 El Capitan, the Disk Utility refused to work in graphical mode for my flash drive (erasing was denied with “wrong name”). But the CLI way went alright !

  18. Seth says:

    Hi,
    I tried a lot of command from here but was not able to repair my 128GB lexar flash drive. I got this error:
    Error: -69825: Wiping volume data to prevent future accidental probing failed
    Can anyone help me on this? Thanks

    • OM says:

      for anyone having problems with erasing the drive with funny errors like this that Seth had, and I had a different -69xxx error, since I couldn’t erase a drive that was partitioned into multiple encrypted time machine backups, and disk utility decided that each logical volume was actually a separate physical device as well… I went to eject a problem volume (the one the current machine uses as a TM backup) since it also wouldn’t eject from the GUI/~DU.app:

      first command:

      diskutil list

      -now I can see what the volume’s logical location is like:
      disk5s2 , which will also give me the device’s location (disk5), s2 refers to the volume/partition of the device disk5. diskutil list shows me that the problem volume is listed as disk7 to the system/gui/disk utility.app, but underneath it tells me its logical location of disk5s2… I’m happy now, because I really needed the device more than volume.

      then use THAT instead of the name if I want to be super cool:
      diskutil eject disk5s2
      also there’s no reason to just use this, since I only really wanted the location of the device (disk5)
      diskutil eject /Volumes/Name\ with\ escaped\ spaces)

      then
      diskutil eraseDisk JHFS+ newDiskNameWhoCares /dev/disk5

      erases all the partitions that appeared as one device in Disk Utility.

  19. Seth says:

    Hi,
    I tried a lot of command from here but was not able to repair my 128GB lexar flash drive. I got this error:
    Error: -69825: Wiping volume data to prevent future accidental probing failed
    Can anyone help me on this? Thanks

  20. Carlos says:

    BTW, if you’re trying to erase the disk and change the partition map, try:

    diskutil eraseDisk FAT32 MYFLASH MBRFormat /dev/disk2

  21. Carlos says:

    Thanks! Works on High Sierra since Apple’s new Disk Utility can’t do crap. Apple Devs are going down the drain these days!

  22. Alex says:

    When trying to erase the disk it comes up with the message “Wiping volume data to prevent future accidental probing failed”. How do I continue?

  23. sachin says:

    /dev/disk2 (external, physical):
    #: TYPE NAME SIZE IDENTIFIER
    0: GUID_partition_scheme *68.7 GB disk2
    1: EFI 209.7 MB disk2s1
    2: Apple_HFS 68.4 GB disk2s2
    can’t format this disk tried lot of time but no help after every format its says could not mount the disk

  24. Edmund Cape says:

    Hi – Despite my double checking the list to identify the right dev to delete, I deleted a drive that had all of my photos. I have four drives in an external Stardom stack. I was having problems with a corrupted drive. Do you have any advice on how to retrieve the files?

    The specific command used: * diskutil reaseDisk HFS+ Erased disk1 *

    I have been working with testDisk, Data Rescue, Stellar, Cisdem and DiskWarrior. There is no way the files are gone; I did not run any kind of scrambling/security erase, nor write any new files to the drive.

    Thank you in advance for your any guidance with how to recover from this specific command.

  25. Sjorre says:

    Hi,

    I am trying to wipe a bootable WIN7 startup disk, but keep on getting the error

    Started erase on disk5
    Unmounting disk
    Error: -69877: Couldn’t open device

  26. Gapin says:

    Thank you very much. That helped me.

  27. Junebug says:

    This worked great, thank you so much for showing me how to erase a hard drive from the command line.

  28. Ken says:

    After this shows up in terminal..what do I do next to have it finally erased so I can use it again? This is what I see.

    diskutil eraseDisk
    Usage: diskutil eraseDisk format name [APM[Format]|MBR[Format]|GPT[Format]]
    MountPoint|DiskIdentifier|DeviceNode
    Completely erase an existing whole disk. All volumes on this disk will be
    destroyed. Ownership of the affected disk is required.
    Format is the specific file system name you want to erase it as (HFS+, etc.).
    Name is the (new) volume name (subject to file system naming restrictions),
    or can be specified as %noformat% to skip initialization (newfs).
    You cannot erase the boot disk.
    Example: diskutil eraseDisk JHFS+

  29. Ryszart says:

    hi!

    I’m just wondering, how do you make your terminal look like that? I don’t see any option in Terminal preferences or in global preferences, apart of Dark mode, which doesnt work for window borders…

  30. Ian says:

    Very recently, I came across a situation where Disk Utility could not erase the HDD. Under the strictest supervision from Apple, I was able to do this from the command line. As it was a Fusion Drive, I was then instructed on how to merge or fuse the two disks – SSD and spinning platter. It all worked perfectly in the end BUT this is NOT for the amateur or faint hearted.

  31. Mark Ross says:

    I have a question that is related. . . sort of.

    Why is Apple holding back from implementing a modern disk file system on their computers? The specs for one of these was offered to them (and basically everyone else) free of charge, quite a few years ago by Sun Microsystems.

    Remember ZFS?

    Would it have been simple to implement? Oh, probably not.

    Were any of the other file systems in use today simple to implement? I’m guessing not.

    Apple charges a higher than normal price for what I will admit is a higher than normal quality machine.

    I just think it’s time the quality of the filing system on the hard drive be brought up to Apple standards. The status quo doesn’t cover it and Apple has never been known to settle for ‘status quo’, anyway.

    So, please, put a few of your brilliant engineers on this, would ya? Failure should not be an option.

    Or, maybe you’re just waiting for hard drives to go away completely? Now, THAT’S an interesting thought.

    Mark Ross
    Apple Customer

    • Sam Cooke says:

      Apple is working on APFS, or Apple File System, but it won’t be used for at least another year. Maybe they will release new Macs in that time that could use it.

  32. Alex says:

    Hello

    If you want to erase and empty the disk (no new file system):

    diskutil erasedisk free %noformat% MBR diskname

  33. Sean says:

    Thanks for that info.
    Is there any speed advantage to this method over the GUI?

    • Paul says:

      No it’s the same erase process, it’s just handled through the command line rather than the Disk Utility GUI app.

  34. Bob says:

    Why publish such dangerous commands which might create disasters for the adventurous amateur.

    • tgrudzin says:

      The spreading of knowledge should never be discourage. If “adventurous amateur” create disasters, then so be it. They will learn something from it. And no great disaster will occur. They might lose ‘everything important to them’. That isn’t a disaster. My story is I hand off USB and other older hard drives. I’m a photogrpaher. I’d like to be sure that previous data is gone. One way I was taught was to hard format a drive in Windows, then do similar in Mac, then again format in Windows, then add the data. For jpgs, this should elimant many software recovery options.

      • Anon says:

        “The spreading of knowledge should never be discourage. If “adventurous amateur” create disasters, then so be it. They will learn something from it.”
        – Couldn’t agree more.
        My understanding of how your trying to do secure deletes: Picture a book that can have either a Contents(PC) or an Index(Mac).
        By deleting data all you do is wipe the reference to the data in the Contents or Index
        By switching Mac or PC formatted all your doing is choosing between “i want an Index/Contents” the pages in the book are all still there and can be easily read. As a bare minimum overwriting the whole disk 7 times is considered just about acceptable.
        There are free and commercial options for ‘secure erase’ – but there are also free and commercial versions of UnDelete, so do try some against each other if its important to you.

  35. Bev in TX says:

    Thanks for the interesting article. I do have one question because I don’t understand the difference between:

    Mac OS Extended Journaled:
    diskutil eraseDisk JHFS+ DiskName /dev/DiskNodeID

    and

    MS-DOS fat32:
    diskutil eraseDisk JHFS+ DiskNameGoesHere /dev/DiskNodeIDHere

    Should the MS-DOS one use FAT32 instead of JHFS+? Or am I misunderstanding something?

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site