This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Removing Unused Tokens from the Database

As you use the WSO2 Identity Server (WSO2 IS), the number of revoked, inactive and expired tokens accumulates in the IDN_OAUTH2_ACCESS_TOKEN table. These tokens are kept in the database for logging and audit purposes, but they can have a negative impact on the server's performance over time. Therefore, it is recommended to clean them periodically in order to enhance the token lookup and to avoid a growing access token table. 

You can use one of the following methods for token cleanup.

Configuring WSO2 Identity Server for token cleanup

WSO2 Identity Server triggers token cleanup during the following instances. 

  1. New token generation
  2. Token refresh
  3. Token revocation

Enable token cleanup by configuring the following properties in the identity.xml file found in the <IS_HOME>/repository/conf/identity folder. 

<!-- token cleanup feature config-->
<TokenCleanup>
    <!-- old access token cleaning feature  -->
    <EnableTokenCleanup>true</EnableTokenCleanup>
    <!-- old access token will be retain in audit table  -->
    <RetainOldAccessToken>true</RetainOldAccessToken>
</TokenCleanup>
PropertyDescription
<EnableTokenCleanup>

Set this property to true to enable token cleanup.

Set it to false to disable token cleanup.

<RetainOldAccessToken>

Set this property to true to move the old, invalid tokens to the Audit table when token cleaning is enabled.

Set it to false if you do not wish to store old tokens in the Audit table.

Using stored procedures for token cleanup

Alternatively, you can also use the provided stored procedures to run a token cleanup task periodically to remove the old and invalid tokens. Follow the instructions below to configure token cleanup using this method. 

Tip: It is safe to run these steps in read-only mode or during a time when traffic on the server is low, but that is not mandatory.

  1. Take a backup of the running database. 
  2. Set up the database dump in a test environment and test it for any issues.

    Tip: We recommend that you test the database dump before the cleanup task as the cleanup can take some time.

  3. Depending on your database, select the appropriate token cleanup script from here and run it on the database dump. This takes a backup of the necessary tables, turns off SQL updates and cleans the database of unused tokens.

  4. Once the cleanup is over, start the WSO2 Identity Server pointing to the cleaned-up database dump and test throughly for any issues. 
    You can also schedule a cleanup task that will be automatically run after a given period of time. Here's an example: