Overview

Skill Level: Any Skill Level

Users: All IBM Cloud account Administrators and Account Owners

Unattached storage volumes are often come as a surprise in every billing cycle. A simple house keeping recipe for all IBM Cloud Account Admins and Owners to cleanup unused storage volumes (block & file) using ibmcloud cli and bash shell scripts.

Ingredients

 

Install IBM Cloud CLI.

For more details refer, Getting started with the IBM Cloud CLI - https://cloud.ibm.com/docs/cli

Step-by-step

  1. Log in

    • Use the ibmcloud login command to log in.

    Note: If you log in with a federated ID, use the –sso option to authenticate with a one time passcode. Or use the –apikey option to authenticate with an API key.

    • Select the target account.

     

    For more details on login, refer the screen shot

    devrecipescs

  2. Listing the Block Storage and File Storage volumes in IBM Cloud account

    To list out all Block Storage volumes in an account :

    ibmcloud sl block volume-list

    To list out all File Storage volumes in an account :

    ibmcloud sl file volume-list

    To list out all unattached/unused Block Storage volumes in an account

    for disk in $(ibmcloud sl block volume-list | awk -v var=" " '(NR>1) {print $1}' ); do count=0; 
    for hosts in $(ibmcloud sl block access-list $disk --sortby id | awk -v var=" " '(NR>1) {print $1}' );
    do count=$((count + 1)); done; if [[ ( $count -eq 0 )]]; then echo $disk; fi; done

     To list out all unattached/unused File Storage volumes in an account

    for disk in $(ibmcloud sl file volume-list | awk -v var=" " '(NR>1) {print $1}' ); do count=0; 
    for hosts in $(ibmcloud sl file access-list $disk --sortby id | awk -v var=" " '(NR>1) {print $1}' );
    do count=$((count + 1)); done; if [[ ( $count -eq 0 )]]; then echo $disk; fi; done
  3. Clean up unused Block Storage in IBM Cloud account

    • Create a file using the touch command. $ touch BlockStorageCleanup
    • Paste the below code to BlockStorageCleanup file
    #!/bin/bash
    for disk in $(ibmcloud sl block volume-list | awk -v var=" " '(NR>1) {print $1}' )
    do
    count=0
    for hosts in $(ibmcloud sl block access-list $disk --sortby id | awk -v var=" " '(NR>1) {print $1}' )
    do
    count=$((count + 1))
    done;
    if [[ ( $count -eq 0 )]];
    then
    ibmcloud sl block volume-cancel $disk --immediate -f;
    fi
    done
    • $ chmod +x BlockStorageCleanup
    • Run ./BlockStorageCleanup
  4. Clean up unused File Storage in IBM Cloud account

    • Create a file using the touch command. $ touch FileStorageCleanup
    • Paste the below code to FileStorageCleanup file
    #!/bin/bash
    for disk in $(ibmcloud sl file volume-list | awk -v var=" " '(NR>1) {print $1}' )
    do
    count=0
    for hosts in $(ibmcloud sl file access-list $disk --sortby id | awk -v var=" " '(NR>1) {print $1}' )
    do
    count=$((count + 1))
    done;
    if [[ ( $count -eq 0 )]];
    then
    ibmcloud sl file volume-cancel $disk --immediate -f;
    fi
    done
    • $ chmod +x FileStorageCleanup
    • Run ./FileStorageCleanup

Join The Discussion