@@ -243,3 +243,114 @@ export def "set-variable" [key: string, value: string, --workspace: string, --ca
243243 }
244244}
245245
246+ export def "del-variable" [key : string , -- workspace : string , -- all = false , -- force = false ] {
247+
248+ # Delete a variable from Terraform Cloud
249+ # If --all is true, delete all variables matching the key pattern
250+ # If --force is true, skip confirmation prompt
251+
252+ let config = get_tf_cloud_config
253+
254+ if ($config.api_token | is-empty ) {
255+ echo " Error: TF_API_TOKEN environment variable not set"
256+ return
257+ }
258+
259+ let organization = get_current_organization
260+
261+ if ($organization | is-empty ) {
262+ echo " Could not determine organization"
263+ return
264+ }
265+
266+ # Get workspace ID
267+ mut workspace_id = " "
268+ try {
269+ let workspace_response = http get -- headers ($config.headers ) ($config.base_url + " /organizations/" + $organization + " /workspaces/" + $workspace )
270+ let workspace_data = $workspace_response | from json | get data
271+ $workspace_id = ($workspace_data | get id )
272+ } catch { |error |
273+ echo " Error getting workspace ID: " + $error
274+ return
275+ }
276+
277+ # Get all variables to find the one(s) to delete
278+ mut variables_to_delete = []
279+ try {
280+ let variables_response = http get -- headers ($config.headers ) ($config.base_url + " /workspaces/" + $workspace_id + " /vars" )
281+ let variables_data = $variables_response | from json | get data
282+
283+ if ($variables_data | is-empty ) {
284+ echo " No variables found in workspace: $workspace"
285+ return
286+ }
287+
288+ if ($all ) {
289+ # Delete all variables matching the key pattern
290+ $variables_to_delete = ($variables_data | where { |var | $var.attributes.key =~ $key })
291+ } else {
292+ # Delete exact match
293+ $variables_to_delete = ($variables_data | where { |var | $var.attributes.key == $key })
294+ }
295+
296+ if (($variables_to_delete | length ) == 0 ) {
297+ echo " No variables found matching: $key"
298+ return
299+ }
300+ } catch { |error |
301+ echo " Error fetching variables: " + $error
302+ return
303+ }
304+
305+ # Show what will be deleted and ask for confirmation
306+ if (($force ) != true ) {
307+ echo " "
308+ echo " WARNING: You are about to delete the following variables from workspace '$workspace':"
309+ echo " ---"
310+
311+ $variables_to_delete | each { |var |
312+ let var_key = $var.attributes.key
313+ let var_value = $var.attributes.value
314+ let var_category = $var.attributes.category
315+ let var_sensitive = $var.attributes.sensitive
316+
317+ echo " Key: $var_key"
318+ echo " Category: $var_category"
319+ if ($var_sensitive ) {
320+ echo " Value: [SENSITIVE - HIDDEN]"
321+ } else {
322+ echo " Value: $var_value"
323+ }
324+ echo " ID: $var.id"
325+ echo " ---"
326+ }
327+
328+ echo " "
329+ let confirm = (input " Are you sure you want to delete these variables? (y/N): " )
330+
331+ if ($confirm | str downcase ) != " y" {
332+ echo " Deletion cancelled."
333+ return
334+ }
335+ }
336+
337+ # Delete each variable
338+ echo " "
339+ echo " Deleting variables..."
340+
341+ $variables_to_delete | each { |var |
342+ let var_id = $var.id
343+ let var_key = $var.attributes.key
344+
345+ try {
346+ let response = http delete -- headers ($config.headers ) ($config.base_url + " /vars/" + $var_id )
347+ echo " ✓ Deleted variable: $var_key (ID: $var_id)"
348+ } catch { |error |
349+ echo " ✗ Error deleting variable $var_key: " + $error
350+ }
351+ }
352+
353+ echo " "
354+ echo " Variable deletion complete."
355+ }
356+
0 commit comments