Question

doctl compute droplet delete xyz when there are <>1 droplets with the name xyz

I need to delete all droplets with the name xyz. However when I run:

doctl compute droplet delete xyz -f

However this only works if there is exactly one droplet with the name xyz.

If there are >1 droplets I get:

Error: There are 2 Droplets with the name "xyz"; please provide a specific Droplet ID. [1111111, 9999999]

If there are 0 droplets I get:

Error: Droplet with the name "xyz" could not be found.

The problem is that I don’t know how many droplets have the name xyz when my script is run.

I tried:

doctl compute droplet delete xyz -f --all

But the --all flag does not exist. What would you recommend if I want to delete all droplets (I want this to work if there are 0, 1, 2, … n droplets, not just if there is exactly one droplet)


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 3, 2024
Accepted Answer

Hello Albert,

Indeed, as the --all flag doesn’t exist for the doctl compute droplet delete command, you can achieve your goal by combining a few commands. You can list all Droplets, filter those with the name ‘xyz’, extract their IDs, and then delete each one. Here’s how you can do it in a shell script or command line:

doctl compute droplet list --format "ID,Name" --no-header | grep " xyz" | awk '{print $1}' | xargs -I {} doctl compute droplet delete {} -f

This command sequence does the following:

  • Lists all Droplets, outputting only their ID and Name without headers.
  • Filters the list to include only those with the name ‘xyz’.
  • Extracts the IDs of these Droplets.
  • Passes each ID to doctl compute droplet delete to delete the Droplets without confirmation prompts.

Note that this is a destructive action so make sure to test this script with caution, especially when using -f to force deletion without confirmation. You can for example only run the first part to view the IDs that will be passed to the delete command:

doctl compute droplet list --format "ID,Name" --no-header | grep " xyz" | awk '{print $1}'

Regarding your suggestion to have a more straightforward way to handle this, I recommend submitting a feature request to the doctl GitHub repository. You can propose the addition of an --all flag or a similar feature that allows users to delete multiple droplets by name more seamlessly. Here’s the link to create a new issue: Submit a Feature Request to doctl.

If you have any more questions or need further assistance, feel free to ask!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel