You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询DigitalOcean指定日期创建的droplet快照及对应ID

DigitalOcean 指定日期Droplet快照ID获取及删除方案

方法一:使用官方doctl命令行工具(操作最简)

  • 前置要求:已完成doctl安装和账号认证
  • 筛选指定日期快照ID:
    如需获取2024-01-01当天创建的Droplet快照,执行命令:
    doctl compute snapshot list --resource-type droplet --format ID,Name,CreatedAt | grep "2024-01-01"
    命令返回结果第一列即为对应快照ID。
  • 批量删除过滤后的快照(操作前务必先核对筛选结果):
    doctl compute snapshot list --resource-type droplet --format ID,CreatedAt | grep "2024-01-01" | awk '{print $1}' | xargs -I {} doctl compute snapshot delete {} -f
    可先将xargs部分替换为echo {}预览待删ID,确认无误后再执行删除逻辑。

方法二:直接调用OpenAPI实现(适合自定义脚本场景)

  • 接口调用前置:提前生成DigitalOcean个人访问令牌,拥有快照读写权限
  • 第一步调用快照列表接口拉取全量Droplet快照数据:
    请求方法为GET,请求头需携带认证字段Authorization: Bearer 你的个人访问令牌,请求地址为https://api.digitalocean.com/v2/snapshots?resource_type=droplet&per_page=200,返回结果中created_at为快照创建时间,id为快照ID。
  • 参考Python过滤删除脚本:
import requests
import os
from datetime import datetime

# 从环境变量读取令牌更安全
DO_TOKEN = os.getenv("DO_TOKEN")
# 替换为你需要的目标日期,格式为YYYY-MM-DD
TARGET_DATE = "2024-01-01"

headers = {"Authorization": f"Bearer {DO_TOKEN}"}
base_url = "https://api.digitalocean.com/v2/snapshots?resource_type=droplet&per_page=200"

to_delete_ids = []
current_url = base_url

# 处理分页数据,避免遗漏快照
while current_url:
    resp = requests.get(current_url, headers=headers).json()
    for snap in resp["snapshots"]:
        if snap["created_at"].startswith(TARGET_DATE):
            to_delete_ids.append(snap["id"])
    current_url = resp.get("links", {}).get("pages", {}).get("next", None)

# 先打印核对待删除ID
print("待删除快照ID列表:", to_delete_ids)

# 确认无误后再取消注释执行删除
# for snap_id in to_delete_ids:
#     requests.delete(f"https://api.digitalocean.com/v2/snapshots/{snap_id}", headers=headers)

重要提示:两种方法操作前都务必先核对筛选出的快照是否符合预期,避免误删重要业务数据。

内容的提问来源于stack exchange,提问作者Muneeshpandi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 17:45:03