37 lines
643 B
Go
37 lines
643 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
|
)
|
|
|
|
func main() {
|
|
token := os.Getenv("HCLOUD_TOKEN")
|
|
if token == "" {
|
|
log.Fatal("HCLOUD_TOKEN missing!")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
client := hcloud.NewClient(hcloud.WithToken(token))
|
|
|
|
servers, err := client.Server.All(ctx)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, server := range servers {
|
|
fmt.Println("Deleting: ", server.Name)
|
|
client.Server.Delete(ctx, server)
|
|
}
|
|
|
|
if len(servers) > 0 {
|
|
fmt.Printf("Success! Deleted %v server(s)", len(servers))
|
|
} else {
|
|
fmt.Println("No servers found, bailing out gracefully")
|
|
}
|
|
}
|