Skip to content

Demo bash script to delete all occurrences of an item #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions delete-occurrences-of-item/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Delete all occurrences of an item

A simple bash script that deletes all occurrences of a Rollbar item via the API.
Be careful with running this script; there is no way to recover the occurrences you delete!

## Usage
```
bash delete_item.sh <item_counter> <read_token> <write_token>
```

- `<item_counter>` The number you see in the url of the item page: https://rollbar.com/<account>/<project>/item/<item_counter>
- `<read_token>` A project access token with *read* scope
- `<write_token>` A project access token with *write* scope

## Dependencies

This script uses [`jq`](https://stedolan.github.io/jq/) and [`curl`](https://curl.haxx.se/).

## Notes

The script loops over the occurrences of the item until it finds any, but sometimes some occurrences are still remain.
If you want to make sure that all occurrences are deleted, just check the UI if there are any occurrences left and run
the script again.
35 changes: 35 additions & 0 deletions delete-occurrences-of-item/delete_item.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
### Delete all occurrences of a Rollbar item

### Parameters
# The item counter from the item url: https://rollbar.com/<account>/<project>/item/<ITEM_COUNTER>/
ITEM_COUNTER="$1"
# A project access token with 'read' scope
READ_TOKEN="$2"
# A project access token with 'write' scope
WRITE_TOKEN="$3"


HOST='https://api.rollbar.com'
API="$HOST/api/1"

item_id=$(curl -s -k "$API/item_by_counter/$ITEM_COUNTER?access_token=$READ_TOKEN" | jq .result.itemId)
page=1
tmp_file=$(mktemp /tmp/rollbar.XXX)

while :
do
printf "\n\nDeleting occurrences of https://rollbar.com/item/$item_id from page $page\n\n"

curl -s -k "$API/item/$item_id/instances?page=$page&access_token=$READ_TOKEN" | \
jq .result.instances[].id > "$tmp_file"

< "$tmp_file" xargs -P20 -I instance_id curl -s -k -XDELETE "$API/instance/instance_id?access_token=$WRITE_TOKEN"

if [ $(wc -l < $tmp_file) -eq 0 ]
then break
else ((page++))
fi
done

rm -rf "$tmp_file"