# API - Optimize Database

**URL:** <https://forums.plex.tv/t/api-optimize-database/892045>\
**Category:** General Discussions\
**Created:** [October 11, 2024, 7:43pm UTC](https://forums.plex.tv/t/api-optimize-database/892045 "2024-10-11T19:43:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickofearth](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@nickofearth](https://forums.plex.tv/u/nickofearth)\
**Post date:** [October 11, 2024, 7:43pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/1 "2024-10-11T19:43:18Z")

</div>

Hello -  
I have a script that backs up the PMS folders, and I wanted to modify the script to optimize the db before I back it up.

I can run the following:  
curl -X POST [http://127.0.0.1:32400/butler/OptimizeDatabase?X-Plex-Token=](http://127.0.0.1:32400/butler/OptimizeDatabase?X-Plex-Token=)

However, I thought it might make more sense (maybe it doesn’t?) to run one that isn’t based on Scheduled Tasks, so I tried:  
curl -X POST [http://127.0.0.1:32400/library/optimize?async=0?X-Plex-Token=](http://127.0.0.1:32400/library/optimize?async=0?X-Plex-Token=)

With the 2nd option, I get a “401 Unauthorized” error.

I am using the same Plex Token for both commands…

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [October 11, 2024, 7:58pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/2 "2024-10-11T19:58:33Z")

</div>

It’s `async=1`

---

<div class="post-metadata">

**Author:** ![nickofearth](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@nickofearth](https://forums.plex.tv/u/nickofearth)\
**Post date:** [October 11, 2024, 8:00pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/3 "2024-10-11T20:00:48Z")

</div>

Hi, dane22 -  
Thank you for the tip - but, I have tried async=0 and async=1 and the result is the same.

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [October 11, 2024, 8:02pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/4 "2024-10-11T20:02:56Z")

</div>

Heh, I forgot one thing as well…

The method is `PUT` and not `POST`

---

<div class="post-metadata">

**Author:** ![nickofearth](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@nickofearth](https://forums.plex.tv/u/nickofearth)\
**Post date:** [October 11, 2024, 8:04pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/5 "2024-10-11T20:04:58Z")

</div>

Just tried:

curl -X PUT [http://127.0.0.1:32400/library/optimize?async=1?X-Plex-Token=](http://127.0.0.1:32400/library/optimize?async=1?X-Plex-Token=)

And the result was the same.

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [October 11, 2024, 8:14pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/6 "2024-10-11T20:14:53Z")

</div>

And you did have a client identifier as a param as well?

`/library/optimize?async=1&X-Plex-Client-Identifier=SoMeRaNdOmStRiNg&X-Plex-Token=XXXXXXXX`

---

<div class="post-metadata">

**Author:** ![pshanew](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pshanew/32/160555_2.png) [@pshanew](https://forums.plex.tv/u/pshanew)\
**Post date:** [October 11, 2024, 8:15pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/7 "2024-10-11T20:15:28Z")

</div>

Try it like this:

`curl -X PUT -H "X-Plex-Token: your_token" http://127.0.0.1:32400/library/optimize?async=1`

This is what worked for me.

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [October 11, 2024, 8:17pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/8 "2024-10-11T20:17:50Z")

</div>

And lastly a small hint…

When digesting endpoints, use the webclient and check the network tab of the debugger in the browser

---

<div class="post-metadata">

**Author:** ![nickofearth](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@nickofearth](https://forums.plex.tv/u/nickofearth)\
**Post date:** [October 11, 2024, 8:49pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/9 "2024-10-11T20:49:49Z")

</div>

I ended up using pshanew’s suggestion, and it works

Thanks dane22 and pshanew!

---

<div class="post-metadata">

**Author:** ![SwiftPanda16](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/swiftpanda16/32/81839_2.png) [@SwiftPanda16](https://forums.plex.tv/u/SwiftPanda16)\
**Post date:** [October 11, 2024, 8:53pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/10 "2024-10-11T20:53:07Z")

</div>

You had two `?` in your query parameters.

---

<div class="post-metadata">

**Author:** ![nickofearth](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@nickofearth](https://forums.plex.tv/u/nickofearth)\
**Post date:** [October 11, 2024, 9:39pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/11 "2024-10-11T21:39:36Z")

</div>

Yeah, that was just the latest iteration… I had tried several variations, so I knew I was messing up somewhere. pshanew’s post educated me on the -H option to pass multiple headers in curl. Also, now I have learned about URL query strings!

Lastly, I am using async=0, because the script pauses until the optimizing is done, which I prefer over async=1 which allows the script to continue immediately. Is there any reason I shouldn’t use async=0?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [January 9, 2025, 9:40pm UTC](https://forums.plex.tv/t/api-optimize-database/892045/12 "2025-01-09T21:40:20Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
