ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Delete All Snapshots on KVM with Virsh

    IT Discussion
    kvm hypervisor virtualization virsh
    2
    8
    4.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by

      With a quick command line, you can automate removal of all snapshots associated with a virtual machine. While you could write the VM name into the command line, you can also, as in my example, set a variable with the name of the VM so that a standard removal command can be used.

      let vm="myVMname"
      for snapshot in $(virsh snapshot-list $vm | tail -n +3 | head -n -1 | cut -d' ' -f2); do virsh snapshot-delete $vm $snapshot; done
      

      It's not very commonly that you would want to automatically remove all snapshots from a VM in this manner. If this becomes a regular part of your routine, making this into a saved script would likely make sense.

      ObsolesceO 1 Reply Last reply Reply Quote 3
      • scottalanmillerS
        scottalanmiller
        last edited by

        If you want to automate this to remove all snaps from all VMs, you can just run this one command...

        for vm in $(virsh list --all | tail -n +3 | head -n +1 | cut -d' '  -f7); do for snapshot in $(virsh snapshot-list $vm | tail -n +3 | head -n -1 | cut -d' ' -f2); do virsh snapshot-delete $vm $snapshot; done; done
        
        1 Reply Last reply Reply Quote 3
        • ObsolesceO
          Obsolesce @scottalanmiller
          last edited by Obsolesce

          And in PowerShell:

          Get-VM VMName | Remove-VMSnapshot -Name *

          scottalanmillerS 2 Replies Last reply Reply Quote 0
          • scottalanmillerS
            scottalanmiller @Obsolesce
            last edited by

            @obsolesce said in Delete All Snapshots on KVM with Virsh:

            And in PowerShell:

            Get-VM VMName | Remove-Checkpoint -Name *

            Well, that's for Hyper-V and isn't PowerShell. PowerShell on KVM won't work that way as those are Hyper-V local commands being called from PowerShell, and not PowerShell itself.

            ObsolesceO 1 Reply Last reply Reply Quote 0
            • scottalanmillerS
              scottalanmiller @Obsolesce
              last edited by

              @obsolesce said in Delete All Snapshots on KVM with Virsh:

              And in PowerShell:

              Get-VM VMName | Remove-Checkpoint -Name *

              My Hyper-V hosts do not have this command. How do you get this to work? I can't find any Microsoft documentation on it, either.

              1 Reply Last reply Reply Quote 0
              • ObsolesceO
                Obsolesce @scottalanmiller
                last edited by

                @scottalanmiller said in Delete All Snapshots on KVM with Virsh:

                @obsolesce said in Delete All Snapshots on KVM with Virsh:

                And in PowerShell:

                Get-VM VMName | Remove-Checkpoint -Name *

                Well, that's for Hyper-V and isn't PowerShell. PowerShell on KVM won't work that way as those are Hyper-V local commands being called from PowerShell, and not PowerShell itself.

                Correct. They are PowerShell commands for Hyper-V.

                1 Reply Last reply Reply Quote 0
                • scottalanmillerS
                  scottalanmiller
                  last edited by

                  Is this a personal alias to Remove-VMSnapshot perhaps?

                  ObsolesceO 1 Reply Last reply Reply Quote 0
                  • ObsolesceO
                    Obsolesce @scottalanmiller
                    last edited by Obsolesce

                    @scottalanmiller said in Delete All Snapshots on KVM with Virsh:

                    Is this a personal alias to Remove-VMSnapshot perhaps?

                    No I had that first but edited it incorrectly. Missed the VM in front.

                    But ya I thought vmcheckpoint was an alias but it's not.

                    Remove-VMSnapshot is correct.

                    So weird, they renamed it to checkpoint, but never changed the PS commands.

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post