Here I’m gonna explain how to recover a datastore that has been deleted by accident.
In order to simulate the senario I delete my datastore that contains some VM files:
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-1024x365.png)
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-1-1024x532.png)
Now just enable the ssh service on our ESXi host:
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-2-1024x492.png)
And connect to the one of esxi host that already connected to that Datastore
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-3-1024x632.png)
Go to the vSphere client and write down the location for the deleted datastore or storage device
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-4-1024x522.png)
If we need to see a summary about our storage devices in CLI mode we can use :
offset="128 2048"; for dev in `esxcfg-scsidevs -l | grep "Console Device:" | awk {'print $3'}`; do disk=$dev; echo $disk;
partedUtil getptbl $disk; { for i in `echo $offset`; do echo "Checking offset found at $i:"; hexdump -n4 -s $((0x100000+(512*$i))) $disk; hexdump -n4 -s $((0x1300000+(512*$i))) $disk; hexdump -C -n 128 -s $((0x130001d + (512*$i))) $disk; done; } | grep -B 1 -A 5 d00d; echo "---------------------"; done
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-5.png)
Now we need to get some info about the our deleted storage device partition table:
partedUtil getptbl /vmfs/devices/disks/{Device identifier}
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-6-1024x168.png)
In next step we should define what is the begining and end of block in VMFS partition by issuing this command:
partedUtil getUsableSectors /vmfs/devices/disks/{Device identifier}
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-7-1024x183.png)
Now it’s time to set the a lable for the partition table:
partedUtil mklabel /vmfs/devices/disks/{Device identifier} gpt
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-8-1024x300.png)
Then we need to create the partition table, but before that we need to get the GUID of a VMFS partition because we lost a VMFS partition, if it was a vSAN partition we would use a vSAN GUID from table below, we can get this info with this command:
partedUtil showGuids
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-9-1024x688.png)
Now we create the partition table :
partedUtil setptbl /vmfs/devices/disks/{Device identifier} gpt "1 2048 {end block in VMFS partition} {Partition GUID} 0"
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-10-1024x169.png)
Now we can run the vmkfstool command to mount the deleted Datastore, and rescan all the storage devices
vmkfstools -V
esxcli storage core adapter rescan --all
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-11-1024x214.png)
Now we should check in vSphere client whether the datastore is back or not
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-12-1024x298.png)
the Datastore is back but the size is not correct, for solving this issue we need to check the vmkernel log:
(it is better to open another ssh console and use tail command as follow and on the first console use vmksfstools -V to see the logs
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-16-1024x163.png)
Now we just add 2047 to the highlighted number and try to create the partition table with new end block :
167770079 + 2047 = 167772126
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-17-1024x194.png)
And check again the datastore if the datastore is inaccessible still we can mount it
![](https://www.vmehrabi.com/wp-content/uploads/2020/12/image-18-1024x625.png)
Now we have access to the data