Delete Old Snapshots and Re-create them with DigitalOcean API

Hello
I’m trying to do something kinda complex, and I was doing it manually before n8n

Describe the issue/error/question

So, I’ve 2 Droplets in DigitalOcean, Mail / Site, and every week I do these steps manually:

  1. I shutdown both of them
  2. take a snapshot of both droplets named with the date taken (ex: mail-06-10-2022 / site-06-10-2022)
  3. Delete the old snapshots
  4. PowerOn the Droplets

So to reproduce that in n8n, I’ve faced a couple of issues:

  1. a first issue is that I have to delete the old droplet snapshots first because if toke the new snapshots and when listing all snapshots, I didn’t figure out how to choose the old snapshots only and delete them.

So to pass this issue, I made a request to [List all Snapshots] then [Delete Snapshots] via DigitalOcean API

  1. a second issue is how to determine if the snapshots complete to Power On the Droplets again

So, I think I have to use [wait] and [IF] to check if the Droplet Snapshot Creation is completed or not, and if not completed [False] then [loop] to the [wait] again and check with [IF] again, and if it complete[True], then [Power_On] the Droplet and send a [Telegram] message
And i don’t know how to make this task, i don’t know how to check if the snapshot completed !

The workflow

Information on your n8n setup

  • n8n version: 0.196.0
  • Database you’re using: SQLite
  • Running n8n via: Docker

Hi @MotazHakim, welcome to the community!

Sounds like quite a complex workflow you’re trying to build here.

Unfortunately, it’s not clear to me which problems exactly you are facing. Perhaps we can break this down and take things step by step.

  1. a first issue is that I have to delete the old droplet snapshots first because if toke the new snapshots and when listing all snapshots, I didn’t figure out how to choose the old snapshots only and delete them.

Are you already reading the old snapshots successfully? How does the JSON data you’re getting from Digital Ocean look like and based on which criteria do you determine whether a snapshot is old and should be deleted?

Thanks for your reply @MutedJam

Well, it’s not that complex.

I’m trying to build a workflow for auto snapshots from Droplets every week
and I don’t wanna keep the old snapshots (NO Retention)

So, every week, there are already 2 snapshots that exist, and i call em: OLD SNAPSHOTS

and I want to start a new snapshot from the 2 Droplets and delete the Old exists ones

the API URL to list all Droplet snapshots

https://api.digitalocean.com/v2/snapshots?resource_type=droplet

and here’s the output:

I tried to filter the output by the created_at parameter, the query might be NOT LIKE date()
and NO luck with that (If You can help with that, then this issue is fixed),
So, I’ve decided to Delete all Droplet snapshots first, then later create new snapshots

and here’s what i build for this section to delete all old snapshots

And when the IF statment is Truem then it goes to takeing snapshots process

So, Overall i’ve managed to build this scenario

I think that’s too many workflows, but it works, so if you have any ideas to shorten it?

I’d say if it works and is readable it’s a good workflow.

However, from looking at the screenshot showing the response from the “List Droplet Snapshots” HTTP Request node it’s not clear to me how you’d identify old snapshots. Are you simply trying to delete all old snapshots, and not those with a minimum age?

If it’s the latter it might be worth splitting out the snapshots array using the Item Lists node, then checking if created_at is older than your minimum age using an IF node.

Yes, I’m deleting all snapshots
Can you please tell me how to make the query of filtering the snapshots with created_at to only get snapshots with less than today’s date ?
and also i don’t know how to use the item lists node

Can you please tell me how to make the query of filtering the snapshots with created_at to only get snapshots with less than today’s date ?
and also i don’t know how to use the item lists node

Hi @MotazHakim, I don’t have a Digital Ocean account, so can’t provide real test data here. However, something like this should do the job I think:

Let me know if you have any questions on the steps in the above workflow.

1 Like

Hi @MutedJam
You are Genius

This is a query i was missing : ={{ $today.minus({days: 3}) }} to check for older snapshots by created_at

1 Like

Glad to hear this helps, thanks so much for confirming!

This example expression uses Luxon, check out the docs for details.