Skip to main content
Volumes are block storage devices that can be attached to instances as boot or data disks, with support for resizing and type changes.

Methods

New

Create a new volume from scratch, an image, or a snapshot.
func (r *VolumeService) New(ctx context.Context, params VolumeNewParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) NewAndPoll(ctx context.Context, params VolumeNewParams, opts ...option.RequestOption) (*Volume, error)
project_id
int64
required
Project ID
region_id
int64
required
Region ID
name
string
required
Volume name
size
int64
required
Volume size in GiB
source
string
required
Volume source: new-volume, image, or snapshot
image_id
string
Image ID (required if source is image)
snapshot_id
string
Snapshot ID (required if source is snapshot)
type_name
string
Volume type: standard, ssd_hiiops, ssd_local, ssd_lowlatency, cold, ultra. Defaults to standard
instance_id_to_attach_to
string
Instance ID to attach volume during creation
attachment_tag
string
Block device attachment tag (use with instance_id_to_attach_to)
lifecycle_policy_ids
[]int64
Snapshot schedule policy IDs
tags
map[string]string
Key-value tags
id
string
Volume UUID
name
string
Volume name
size
int64
Volume size in GiB
status
string
Volume status: available, in-use, creating, deleting, error, etc.
volume_type
string
Volume storage type
bootable
bool
Whether the volume is bootable
attachments
[]VolumeAttachment
List of instance attachments

Update

Rename a volume or update tags.
func (r *VolumeService) Update(ctx context.Context, volumeID string, params VolumeUpdateParams, opts ...option.RequestOption) (*Volume, error)
name
string
New volume name
tags
TagUpdateMap
Tags to add, update, or remove

List

List volumes with optional filtering.
func (r *VolumeService) List(ctx context.Context, params VolumeListParams, opts ...option.RequestOption) (*pagination.OffsetPage[Volume], error)
func (r *VolumeService) ListAutoPaging(ctx context.Context, params VolumeListParams, opts ...option.RequestOption) *pagination.OffsetPageAutoPager[Volume]
bootable
bool
Filter by bootable status
has_attachments
bool
Filter by attachment presence
instance_id
string
Filter by attached instance ID
name_part
string
Filter by name substring
limit
int64
Maximum results per page
offset
int64
Results to skip

Get

Retrieve detailed information about a volume.
func (r *VolumeService) Get(ctx context.Context, volumeID string, query VolumeGetParams, opts ...option.RequestOption) (*Volume, error)

Delete

Delete a volume and optionally its snapshots.
func (r *VolumeService) Delete(ctx context.Context, volumeID string, params VolumeDeleteParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) DeleteAndPoll(ctx context.Context, volumeID string, params VolumeDeleteParams, opts ...option.RequestOption) error
snapshots
string
Comma-separated list of snapshot IDs to delete with the volume
Volume must be in available state to be deleted.

AttachToInstance

Attach a volume to an instance.
func (r *VolumeService) AttachToInstance(ctx context.Context, volumeID string, params VolumeAttachToInstanceParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) AttachToInstanceAndPoll(ctx context.Context, volumeID string, params VolumeAttachToInstanceParams, opts ...option.RequestOption) error
instance_id
string
required
Instance UUID
attachment_tag
string
Block device attachment tag

DetachFromInstance

Detach a volume from an instance.
func (r *VolumeService) DetachFromInstance(ctx context.Context, volumeID string, params VolumeDetachFromInstanceParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) DetachFromInstanceAndPoll(ctx context.Context, volumeID string, params VolumeDetachFromInstanceParams, opts ...option.RequestOption) error
instance_id
string
required
Instance UUID

Resize

Increase the size of a volume.
func (r *VolumeService) Resize(ctx context.Context, volumeID string, params VolumeResizeParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) ResizeAndPoll(ctx context.Context, volumeID string, params VolumeResizeParams, opts ...option.RequestOption) (*Volume, error)
size
int64
required
New size in GiB (must be greater than current size)

ChangeType

Change the volume type.
func (r *VolumeService) ChangeType(ctx context.Context, volumeID string, params VolumeChangeTypeParams, opts ...option.RequestOption) (*Volume, error)
volume_type
string
required
New volume type: standard or ssd_hiiops
Volume must not have snapshots to change type.

RevertToLastSnapshot

Revert a volume to its last snapshot.
func (r *VolumeService) RevertToLastSnapshot(ctx context.Context, volumeID string, body VolumeRevertToLastSnapshotParams, opts ...option.RequestOption) error
Volume must be in available state to be reverted.

Examples

Create an Empty Volume

import (
    "context"
    "github.com/G-Core/gcore-go"
    "github.com/G-Core/gcore-go/cloud"
    "github.com/G-Core/gcore-go/shared/constant"
    "github.com/G-Core/gcore-go/option"
)

client := gcore.NewClient(
    option.WithCloudProjectID(projectID),
    option.WithCloudRegionID(regionID),
)

volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(), 
    cloud.VolumeNewParams{
        OfNewVolume: &cloud.VolumeNewParamsBodyNewVolume{
            Source:   constant.ValueOf[constant.NewVolume](),
            Name:     "data-volume",
            Size:     100, // 100 GB
            TypeName: "ssd_hiiops",
            Tags:     map[string]string{"purpose": "database"},
        },
    },
)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Created volume: %s\n", volume.ID)

Create Volume from Image

volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(), 
    cloud.VolumeNewParams{
        OfImage: &cloud.VolumeNewParamsBodyImage{
            Source:   constant.ValueOf[constant.Image](),
            Name:     "boot-volume",
            ImageID:  "image-uuid",
            Size:     20,
            TypeName: "standard",
        },
    },
)

Create and Attach Volume

volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(), 
    cloud.VolumeNewParams{
        OfNewVolume: &cloud.VolumeNewParamsBodyNewVolume{
            Source:               constant.ValueOf[constant.NewVolume](),
            Name:                 "attached-volume",
            Size:                 50,
            InstanceIDToAttachTo: gcore.String(instanceID),
        },
    },
)

List All Volumes

volumes, err := client.Cloud.Volumes.List(context.Background(), 
    cloud.VolumeListParams{},
)
if err != nil {
    log.Fatal(err)
}

for _, vol := range volumes.Results {
    fmt.Printf("Volume: %s, Size: %d GB, Status: %s\n", 
        vol.Name, vol.Size, vol.Status)
}

List Volumes Attached to Instance

volumes, err := client.Cloud.Volumes.List(context.Background(), 
    cloud.VolumeListParams{
        InstanceID: gcore.String(instanceID),
    },
)

Attach Volume to Instance

err := client.Cloud.Volumes.AttachToInstanceAndPoll(context.Background(), 
    volumeID,
    cloud.VolumeAttachToInstanceParams{
        InstanceID: instanceID,
    },
)
if err != nil {
    log.Fatal(err)
}

Detach Volume from Instance

err := client.Cloud.Volumes.DetachFromInstanceAndPoll(context.Background(), 
    volumeID,
    cloud.VolumeDetachFromInstanceParams{
        InstanceID: instanceID,
    },
)

Resize a Volume

volume, err := client.Cloud.Volumes.ResizeAndPoll(context.Background(), 
    volumeID,
    cloud.VolumeResizeParams{
        Size: 200, // Expand to 200 GB
    },
)

Change Volume Type

volume, err := client.Cloud.Volumes.ChangeType(context.Background(), 
    volumeID,
    cloud.VolumeChangeTypeParams{
        VolumeType: cloud.VolumeChangeTypeParamsVolumeTypeSsdHiiops,
    },
)

Delete Volume

err := client.Cloud.Volumes.DeleteAndPoll(context.Background(), 
    volumeID,
    cloud.VolumeDeleteParams{},
)

Common Use Cases

Create Database Volume with Snapshots

volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(), 
    cloud.VolumeNewParams{
        OfNewVolume: &cloud.VolumeNewParamsBodyNewVolume{
            Source:             constant.ValueOf[constant.NewVolume](),
            Name:               "postgres-data",
            Size:               500,
            TypeName:           "ssd_hiiops",
            LifecyclePolicyIDs: []int64{policyID}, // Daily snapshots
            Tags:               map[string]string{
                "database": "postgres",
                "environment": "production",
            },
        },
    },
)

Clone Volume from Snapshot

volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(), 
    cloud.VolumeNewParams{
        OfSnapshot: &cloud.VolumeNewParamsBodySnapshot{
            Source:     constant.ValueOf[constant.Snapshot](),
            Name:       "cloned-volume",
            SnapshotID: snapshotID,
        },
    },
)

Build docs developers (and LLMs) love