How to Detach Volumes

Validated on 2 Oct 2019 • Last edited on 18 Dec 2024

Volumes are network-based block devices that provide additional data storage for Droplets. You can move them between Droplets, create disk images of them, and resize them at any time.

To remove a volume from a Droplet, you can either detach the volume or delete the volume.

  • Detaching a volume removes the volume from its current Droplet. You can attach the volume and all of its contents to a different Droplet as needed.
  • Deleting a volume permanently destroys the volume and its contents. This action cannot be reversed.

Detach a Volume Using Automation

Before detaching a volume, unmount it to avoid data loss.

How to Detach a Volume Using the DigitalOcean CLI
  1. Install doctl, the official DigitalOcean CLI.
  2. Create a personal access token and save it for use with doctl.
  3. Use the token to grant doctl access to your DigitalOcean account.
    doctl auth init
  4. Finally, run doctl compute volume-action detach. Basic usage looks like this, but you can read the usage docs for more details:
    doctl compute volume-action detach <volume-id> <droplet-id> [flags]
    The following example detaches a volume with the UUID f81d4fae-7dec-11d0-a765-00a0c91e6bf6 from a Droplet with the ID 386734086:
    doctl compute volume-action detach f81d4fae-7dec-11d0-a765-00a0c91e6bf6 386734086

To detach a volume using the API, use the volume actions endpoints and set the type field to detach.

How to Detach a Volume Using the DigitalOcean API
  1. Create a personal access token and save it for use with the API.
  2. Send a POST request to https://api.digitalocean.com/v2/volumes/{volume_id}/actions.

cURL

Using cURL:

# Attach a Volume to a Droplet by ID
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
  -d '{"type": "attach", "droplet_id": 11612190, "region": "nyc1", "tags":["aninterestingtag"]}' \
  "https://api.digitalocean.com/v2/volumes/7724db7c-e098-11e5-b522-000f53304e51/actions"

# Remove a Volume from a Droplet by ID
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
  -d '{"type": "detach", "droplet_id": "11612190", "region": "nyc1"}' \
  "https://api.digitalocean.com/v2/volumes/7724db7c-e098-11e5-b522-000f53304e51/actions"

# Resize a Volume
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
  -d '{"type":"resize","size_gigabytes": 100, "region":"nyc1"}' \
  "https://api.digitalocean.com/v2/volumes/7724db7c-e098-11e5-b522-000f53304e51/actions"

Go

Using Godo, the official DigitalOcean API client for Go:

import (
    "context"
    "os"

    "github.com/digitalocean/godo"
)

func main() {
    token := os.Getenv("DIGITALOCEAN_TOKEN")

    client := godo.NewFromToken(token)
    ctx := context.TODO()

  // Attach a Volume to a Droplet by ID
    action, _, err := client.StorageActions.Attach(ctx, "7724db7c-e098-11e5-b522-000f53304e51", 11612190)

  // Remove a Volume from a Droplet by ID
  // action, _, err := client.StorageActions.Detach(ctx, "7724db7c-e098-11e5-b522-000f53304e51")

  // Resize a Volume
  // action, _, err := client.StorageActions.Resize(ctx, "7724db7c-e098-11e5-b522-000f53304e51", 100, "nyc1")
}

Ruby

Using DropletKit, the official DigitalOcean API client for Ruby:

require 'droplet_kit'
token = ENV['DIGITALOCEAN_TOKEN']
client = DropletKit::Client.new(access_token: token)

# Attach a Volume to a Droplet by ID
client.volume_actions.attach(volume_id:'7724db7c-e098-11e5-b522-000f53304e51', droplet_id: 11612190, region: 'nyc1'


# Remove a Volume from a Droplet by ID
# client.volume_actions.detach(volume_id:'7724db7c-e098-11e5-b522-000f53304e51', droplet_id: 11612190, region: 'nyc1'

Python

Using PyDo, the official DigitalOcean API client for Python:

import os
from pydo import Client

client = Client(token=os.environ.get("DIGITALOCEAN_TOKEN"))

req = {
  "type": "attach",
  "droplet_id": 11612190,
  "region": "nyc1",
  "tags": [
    "aninterestingtag"
  ]
}

resp = client.volume_actions.post_by_id(volume_id="7724db7c", body=req)

Detach a Volume Using the Control Panel

Before detaching a volume, unmount it to avoid data loss.

You can detach a volume from the volume’s More menu, which is available on the account-level Volumes page.

Volumes more menu

Select Detach from Droplet to detach the volume. You can then attach the volume to a different Droplet as needed.

If you detach an auto-mounted volume, you should also delete the corresponding systemd config file from the Droplet, which is at /etc/systemd/system/mnt-volume_*.mount.

We can't find any results for your search.

Try using different keywords or simplifying your search terms.