How to delete deployments? #85000
-
Select Topic AreaQuestion BodyCould someone help me with this question? I have a repository where there is a lot of implementation history. I would like to clean it. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 6 replies
-
|
It appears you can use GitHub CLI for that too: gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/OWNER/REPO/deployments/DEPLOYMENT_IDOr curl: curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/deployments/DEPLOYMENT_IDThis may help: https://docs.github.com/en/rest/deployments/deployments |
Beta Was this translation helpful? Give feedback.
-
|
In case others come across this like me, I wanted to clear all the deployments for a repo but deleting the env and recreating it with the same name kept them all. The below will fetch 100 deployments at a time and then delete them with the NOTE: this will do it for each environment curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/deployments\?per_page\=100 | jq -r '.[].id' | xargs -P 0 -I {} curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/deployments/\{\} |
Beta Was this translation helpful? Give feedback.
-
|
I used this script to delete all deployments for an environment |
Beta Was this translation helpful? Give feedback.
-
|
I recently ran into something similar and learned that I had to mark deployments as inactive before deleting them. This script marks all deployments as inactive, and deletes all deployments, 100 at a time. REPO="OWNER_NAME/REPO_NAME"
for ID in $(gh api --method GET "/repos/$REPO/deployments?per_page=100" | jq -r ".[] | .id")
do
gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" --method POST /repos/$REPO/deployments/$ID/statuses -f "state=inactive"
gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" --method DELETE /repos/$REPO/deployments/$ID
done |
Beta Was this translation helpful? Give feedback.
-
|
Why is this not possible quickly from the UI? |
Beta Was this translation helpful? Give feedback.
-
|
FYI, a clean history can't be achieved until #123659 is resolved. |
Beta Was this translation helpful? Give feedback.
-
|
If the deployments are still active, you first have to set them inactive. #!/bin/bash
REPO="org/repo"
TOKEN="YOUR_TOKEN_HERE"
for deployment in $(curl -H "authorization: token $TOKEN" https://api.github.com/repos/$REPO/deployments?per_page=100 | jq -r '.[] | @base64'); do
DEPLOYMENT_ID=$(echo "$deployment" | base64 --decode | jq -r '.id')
echo "$DEPLOYMENT_ID"
curl "https://api.github.com/repos/$REPO/deployments/$DEPLOYMENT_ID/statuses" \
-X POST \
-d '{"state":"inactive"}' \
-H 'accept: application/vnd.github.ant-man-preview+json' \
-H "authorization: token $TOKEN"
done
for deployment in $(curl -H "authorization: token $TOKEN" https://api.github.com/repos/$REPO/deployments | jq -r '.[] | @base64'); do
DEPLOYMENT_ID=$(echo "$deployment" | base64 --decode | jq -r '.id')
curl "https://api.github.com/repos/$REPO/deployments/$DEPLOYMENT_ID" \
-X DELETE \
-H "authorization: token $TOKEN"
done |
Beta Was this translation helpful? Give feedback.
-
Thanks. ㅤ REPO="OWNER_NAME/REPO_NAME"
for ID in $(gh api "repos/$REPO/deployments?per_page=100" | jq -r '.[] | .id' | tr -d '\r'); do
STATUS=$(gh api "repos/$REPO/deployments/$ID/statuses" | jq -r '.[0].state')
echo "Deployment ID: $ID | Status: $STATUS"
if [ "$STATUS" = "inactive" ]; then
echo "→ Deleting inactive deployment ID: $ID"
gh api -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--method DELETE "repos/$REPO/deployments/$ID"
fi
done |
Beta Was this translation helpful? Give feedback.
It appears you can use GitHub CLI for that too:
Or curl:
This may help: https://docs.github.com/en/rest/deployments/deployments