# Clear all pending devices with script

**URL:** <https://hub.mender.io/t/clear-all-pending-devices-with-script/3295>\
**Category:** General Discussions\
**Created:** [March 3, 2021, 4:49pm UTC](https://hub.mender.io/t/clear-all-pending-devices-with-script/3295 "2021-03-03T16:49:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bender](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bender/32/847_2.png) [@bender](https://hub.mender.io/u/bender)\
**Post date:** [March 3, 2021, 4:49pm UTC](https://hub.mender.io/t/clear-all-pending-devices-with-script/3295/1 "2021-03-03T16:49:25Z")

</div>

I got fed up with the clearing of pending devices all the time during development/testing and I didn’t want to use the reject option from the web ui. So playing a bit with the API from bash I got this:

```auto
MENDER_SERVER_URI='https://hosted.mender.io';
MENDER_SERVER_USER='[YOUR_USERNAME HERE]';
JWT=$(curl -X POST -u $MENDER_SERVER_USER $MENDER_SERVER_URI/api/management/v1/useradm/auth/login);
curl -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $JWT" -d '{"status": "pending"}' -X POST https://hosted.mender.io/api/management/v2/devauth/devices/search?per_page=500 | jq '[.[] | {id}]' | grep id | sed -r 's/\"//g' | sed -r 's/\ //g' | cut -d':' -f2 | while read i; do \
    curl \
    -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -H "Authorization: Bearer $JWT" \
    -X DELETE https://hosted.mender.io/api/management/v2/devauth/devices/{$i}; done

```

Which decommissions/dismisses the latest 500 pending devices.

The above as an oneliner:

```auto
sudo apt-get install curl jq && MENDER_SERVER_URI='https://hosted.mender.io' && MENDER_SERVER_USER='[YOUR_USERNAME HERE]' && JWT=$(curl -X POST -u $MENDER_SERVER_USER $MENDER_SERVER_URI/api/management/v1/useradm/auth/login) && curl -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer $JWT" -d '{"status": "pending"}' -X POST https://hosted.mender.io/api/management/v2/devauth/devices/search?per_page=500 | jq '[.[] | {id}]' | grep id | sed -r 's/\"//g' | sed -r 's/\ //g' | cut -d':' -f2 | while read i; do curl -H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer $JWT" -X DELETE https://hosted.mender.io/api/management/v2/devauth/devices/{$i}; done

```

What the above commands do is authenticate with the server, get the list of (last 500) pending devices, and calls the decommission command for each one in a bash-based while loop. Apart from the loop the rest are REST API calls. The script was used in Ubuntu but could easily be modified (curl, jq and while parts) to work in your OS.

I am happy now 🙂

---

<div class="post-metadata">

**Author:** ![drewmoseley](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/drewmoseley/32/47_2.png) [@drewmoseley](https://hub.mender.io/u/drewmoseley)\
**Post date:** [March 3, 2021, 5:55pm UTC](https://hub.mender.io/t/clear-all-pending-devices-with-script/3295/2 "2021-03-03T17:55:01Z")

</div>

Nice. Thanks for posting this.

---

<div class="post-metadata">

**Author:** ![bender](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bender/32/847_2.png) [@bender](https://hub.mender.io/u/bender)\
**Post date:** [March 3, 2021, 7:45pm UTC](https://hub.mender.io/t/clear-all-pending-devices-with-script/3295/3 "2021-03-03T19:45:43Z")

</div>

Could be nicer but does the work 😉

---

<div class="post-metadata">

**Author:** ![oleorhagen](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/oleorhagen/32/297_2.png) [@oleorhagen](https://hub.mender.io/u/oleorhagen)\
**Post date:** [March 4, 2021, 8:10am UTC](https://hub.mender.io/t/clear-all-pending-devices-with-script/3295/4 "2021-03-04T08:10:31Z")

</div>

> [@bender](#):
>
> ```auto
> sudo apt-get install curl jq;
> MENDER_SERVER_URI=‘https://hosted.mender.io’;
> MENDER_SERVER_USER=’[YOUR_USERNAME HERE]’;
> JWT=$(curl -X POST -u $MENDER_SERVER_USER $MENDER_SERVER_URI/api/management/v1/useradm/auth/login);
> curl -H “Content-Type: application/json” -H “Accept: application/json” -H “Authorization: Bearer $JWT” -d ‘{“status”: “pending”}’ -X POST https://hosted.mender.io/api/management/v2/devauth/devices/search?per_page=500 | jq ‘[.[] | {id}]’ | grep id | sed -r ‘s/"//g’ | sed -r ‘s/\ //g’ | cut -d’:’ -f2 | while read i; do
> curl
> -H ‘Content-Type: application/json’
> -H ‘Accept: application/json’
> -H “Authorization: Bearer $JWT”
> -X DELETE [https://hosted.mender.io/api/management/v2/devauth/devices/{$i};](https://hosted.mender.io/api/management/v2/devauth/devices/%7B%24i%7D;) done
> 
> ```

Just added the script in a code-block for future viewers 🙂  
Thank you @bender

---

<div class="post-metadata">

**Author:** ![bender](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bender/32/847_2.png) [@bender](https://hub.mender.io/u/bender)\
**Post date:** [March 4, 2021, 9:27am UTC](https://hub.mender.io/t/clear-all-pending-devices-with-script/3295/5 "2021-03-04T09:27:16Z")

</div>

Was looking for this but tried the triple backticks and the preview got blank so I didn’t try it. Fixed the top post. Thanks!
