How to Detach Volumes

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

To detach a volume via the command-line, follow these steps:

  1. Install doctl, the DigitalOcean command-line tool.

  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, detach a volume with doctl compute volume-action detach. The basic usage looks like this, but you'll want to 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

To detach a volume using the DigitalOcean API, follow these steps:

  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

    To detach a volume with cURL, call:

    
                    # 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

    Go developers can use Godo, the official DigitalOcean V2 API client for Go. To detach a volume with Godo, use the following code:

    
                    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

    Ruby developers can use DropletKit, the official DigitalOcean V2 API client for Ruby. To detach a volume with DropletKit, use the following code:

    
                    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

    
                    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.