Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Delete leftover Datomic resources

Delete Leftover Datomic Cloud Resources

If you delete a Datomic Cloud system, it will leave some resources in AWS. To entirely delete all resources created by Datomic Cloud, you must follow these steps. Because we love automation, this script performs all these steps for you so you don't need to navigate the AWS Console UI.



Simply replace your-datomic-system-name in the below bash command.

clojure -Sdeps '{:deps {delete {:git/url "" :sha "eb0262673a05bdb13d2bcd56d36cc9e90eca6e50"}}}' -m delete-datomic-system your-datomic-system-name
(ns delete-datomic-system
[clojure.string :as str]
[ :as sh]
[ :as json]))
(defn json-cmd
([args] (json-cmd args nil))
([args opts]
(let [r (apply sh/sh (concat args (mapcat identity opts)))]
(if (= 0 (:exit r))
(json/read-str (:out r))
(println "Error:" (:err r))))))
(defn parse-arn
(let [parts (drop 1 (str/split arn-str #":" 6))]
(zipmap [:partition :service :region :account-id :resource] parts)))
;; Delete docs
(defn delete-s3-bucket
(sh/sh "aws" "s3" "rb" (str "s3://" s3-bucket) "--force"))
(defn delete-ddb-table
(sh/sh "aws" "dynamodb" "delete-table" "--table-name" table-name))
(defn delete-efs
(sh/sh "aws" "efs" "delete-file-system" "--file-system-id" fs-id))
(defn deregister-dynamo-scalable-target
(let [deregister (fn [dimension]
(sh/sh "aws" "application-autoscaling" "deregister-scalable-target"
"--service-namespace" "dynamodb"
"--scalable-dimension" dimension
"--resource-id" (format "table/datomic-%s" datomic-system)))]
(deregister "dynamodb:table:WriteCapacityUnits")
(deregister "dynamodb:table:ReadCapacityUnits")))
(defn get-datomic-resource-arns
(let [r (json-cmd ["aws" "resourcegroupstaggingapi" "get-resources" "--tag-filters"
(format "Key=datomic:system,Values=%s" system-name)])]
(when r
(map (comp parse-arn #(get % "ResourceARN")) (get r "ResourceTagMappingList")))))
(defn delete-leftover-datomic-resources-from-arns
[system-name parsed-arns]
(let [resources-by-service (group-by :service parsed-arns)]
;; delete s3 buckets
(doseq [{:keys [resource]} (get resources-by-service "s3")]
(println "Deleting S3 bucket" resource)
(delete-s3-bucket resource))
;; delete dynamo tables
(doseq [{:keys [resource]} (get resources-by-service "dynamodb")
:let [[_ table-name] (str/split resource #"\/" 2)]]
(println "Deleting ddb table" table-name)
(delete-ddb-table table-name))
;; delete efs
(doseq [{:keys [resource]} (get resources-by-service "elasticfilesystem")
:let [[_ fs-id] (str/split resource #"\/" 2)]]
(println "Deleting efs " fs-id)
(delete-efs fs-id))
(println "Deregistering dynamodb application-autoscaling")
(deregister-dynamo-scalable-target system-name)))
(defn delete-leftover-datomic-system-resources
(->> (get-datomic-resource-arns system-name)
(delete-leftover-datomic-resources-from-arns system-name)))
(defn system-exit!
"Exit the process."
[status message]
(when (and message (not (str/blank? message)))
(binding [*out* (if (= 0 status) *out* *err*)]
(println message)))
(System/exit status))
(defn -main
[& args]
(let [datomic-system-name (first args)]
(if datomic-system-name
(do (delete-leftover-datomic-system-resources datomic-system-name)
(system-exit! 1 "Missing datomic system name."))))
{:paths ["."]
:deps {org.clojure/data.json {:mvn/version "0.2.6"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment