This documentation is for WSO2 API Manager 2.6.0. View documentation for the latest release.

All docs This doc

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Take a backup of the running database. 

  2. Set up the database dump in a test environment and test it for any issues.

    For more information on setting up a database dump, go to the MySQL, SQL Server, and Oracle official documentation.

    Tip

    Tip: We recommend you to 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.

    Select the `token-cleanup` script to clean up the tokens, the `sessiondata-cleanup` script to cleanup the session data and the `registry-cleanup` script to clean up the registry unused data.

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

    Localtab Group
    Localtab
    activetrue
    titleMySQL
    Code Block
    USE 'WSO2AM_DB';
    DROP EVENT IF EXISTS 'cleanup_tokens_event';
    CREATE EVENT 'cleanup_tokens_event'
        ON SCHEDULE
          EVERY 1 WEEK STARTS '2018-01-01 00:00.00'
        DO
          CALL 'WSO2AM_DB'.'cleanup_tokens'();
    
    -- 'Turn on the event_scheduler'
    SET GLOBAL event_scheduler = ON;
    Localtab
    titleSQL Server
    Code Block
    USE WSO2AM_DB ;  
    GO  
    -- Creates a schedule named CleanupTask.   
    -- Jobs that use this schedule execute every day when the time on the server is 01:00.   
    EXEC sp_add_schedule  
        @schedule_name = N'CleanupTask' ,  
        @freq_type = 4,  
        @freq_interval = 1,  
        @active_start_time = 010000 ;  
    GO  
    -- attaches the schedule to the job BackupDatabase  
    EXEC sp_attach_schedule  
       @job_name = N'BackupDatabase',  
       @schedule_name = N'CleanupTask' ;  
    GO

    Replace WSO2AM_DB with the name of your API Manager database in the above script.

Registry database cleanup

Creating and updating APIs, adding tags and ratings cause different registry properties to be added to the Registry database(Registry DB) in WSO2 API-M. When Registry Versioning is enabled, older properties are persisted in the database leading to unnecessary record growth in the Registry tables in the database. This directly affects the performance of the product if the number of records increase beyond a certain amount. Due to this limitation, registry versioning is disabled by default in WSO2 API Manager 3.0.0 onwards to prevent unnecessary database growth.

In WSO2 API-M versions preceding 3.0.0 (2.x.x and 1.x.x), Registry Versioning is enabled by default, therefore we have to cleanup the Registry DB if there are large tables with millions of records.

You can cleanup the following tables in the Registry DB without affecting the functionality of the product.

  • REG_LOG
  • REG_PROPERTY
  • REG_RESOURCE_PROPERTY
  • REG_TAG
  • REG_RESOURCE_TAG


Info

You can always take a backup of the Registry database before the cleanup as a precaution for any issues that might occur during cleanup.

Note
titleBefore you begin

Remember to shut down the server and stop any DB transactions before the database cleanup. This is to prevent any inconsistencies that could happen with new data that gets added to the DB during the cleanup.


The Cleanup scripts for the Registry DB tables are given below.

REG_LOG Table Cleanup

Code Block
languagesql
titleDB Types: H2, DB2, MySQL, MSSQL, Oracle and Postgresql
CREATE TABLE REG_LOG_IDS_TO_KEEP (
REG_LOG_ID INTEGER,
REG_TENANT_ID INTEGER
);

INSERT INTO REG_LOG_IDS_TO_KEEP (REG_LOG_ID, REG_TENANT_ID)
SELECT MAX(REG_LOG_ID) AS REG_LOG_ID, REG_TENANT_ID FROM REG_LOG GROUP BY REG_PATH, REG_TENANT_ID;

DELETE FROM REG_LOG WHERE REG_LOG_ID NOT IN (SELECT REG_LOG_ID FROM REG_LOG_IDS_TO_KEEP);
DROP TABLE REG_LOG_IDS_TO_KEEP;

DELETE FROM REG_LOG WHERE REG_ACTION = 7;


REG_PROPERTY and REG_RESOURCE_PROPERTY Table Cleanup

Code Block
languagesql
titleDB types: H2, MSSQL and Postgresql
CREATE TABLE TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID INTEGER);

-- Extract resource property (ID) created when versioning is disabled --

INSERT INTO TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID)
SELECT REG_PROPERTY_ID
FROM REG_RESOURCE_PROPERTY
WHERE REG_PATH_ID IN
    (SELECT REG_PATH_ID
        FROM REG_RESOURCE);

-- Extract resource property (ID) created when versioning is enabled --

INSERT INTO TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID)
SELECT REG_PROPERTY_ID
FROM REG_RESOURCE_PROPERTY
WHERE REG_VERSION IN
    (SELECT REG_VERSION
        FROM REG_RESOURCE);

-- Drop the foreign key constraint --

ALTER TABLE REG_RESOURCE_PROPERTY DROP CONSTRAINT REG_RESOURCE_PROPERTY_FK_BY_TAG_ID;

-- delete all unwanted REG_RESOURCE_PROPERTY entries --

DELETE
FROM REG_RESOURCE_PROPERTY
WHERE REG_PROPERTY_ID NOT IN
    (SELECT REG_PROPERTY_ID
    FROM TEMP_REG_RESOURCE_PROPERTY_ID);

-- delete all unwanted REG_PROPERTY entries --

DELETE
FROM REG_PROPERTY
WHERE REG_ID NOT IN
    (SELECT REG_PROPERTY_ID
    FROM TEMP_REG_RESOURCE_PROPERTY_ID);

-- Insert back the foreign key constraint --

ALTER TABLE REG_RESOURCE_PROPERTY ADD CONSTRAINT REG_RESOURCE_PROPERTY_FK_BY_TAG_ID FOREIGN KEY (REG_PROPERTY_ID, REG_TENANT_ID) REFERENCES REG_PROPERTY (REG_ID, REG_TENANT_ID);

-- drop temporary table --

DROP TABLE TEMP_REG_RESOURCE_PROPERTY_ID;
Code Block
languagesql
titleDB types: MySQL
CREATE TABLE TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID INTEGER);

-- Extract resource property (ID) created when versioning is disabled --

INSERT INTO TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID)
SELECT REG_PROPERTY_ID
FROM REG_RESOURCE_PROPERTY
WHERE REG_PATH_ID IN
    (SELECT REG_PATH_ID
        FROM REG_RESOURCE);

-- Extract resource property (ID) created when versioning is enabled --

INSERT INTO TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID)
SELECT REG_PROPERTY_ID
FROM REG_RESOURCE_PROPERTY
WHERE REG_VERSION IN
    (SELECT REG_VERSION
        FROM REG_RESOURCE);

-- Drop the foreign key constraint --

ALTER TABLE REG_RESOURCE_PROPERTY DROP FOREIGN KEY REG_RESOURCE_PROPERTY_FK_BY_TAG_ID;

-- delete all unwanted REG_RESOURCE_PROPERTY entries --

DELETE
FROM REG_RESOURCE_PROPERTY
WHERE REG_PROPERTY_ID NOT IN
    (SELECT REG_PROPERTY_ID
    FROM TEMP_REG_RESOURCE_PROPERTY_ID);

-- delete all unwanted REG_PROPERTY entries --

DELETE
FROM REG_PROPERTY
WHERE REG_ID NOT IN
    (SELECT REG_PROPERTY_ID
    FROM TEMP_REG_RESOURCE_PROPERTY_ID);

-- Insert back the foreign key constraint --

ALTER TABLE REG_RESOURCE_PROPERTY ADD CONSTRAINT REG_RESOURCE_PROPERTY_FK_BY_TAG_ID FOREIGN KEY (REG_PROPERTY_ID, REG_TENANT_ID) REFERENCES REG_PROPERTY (REG_ID, REG_TENANT_ID);

-- drop temporary table --

DROP TABLE TEMP_REG_RESOURCE_PROPERTY_ID;
Code Block
languagesql
titleDB types: DB2 and Oracle
CREATE TABLE TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID INTEGER);

-- Extract resource property (ID) created when versioning is disabled --

INSERT INTO TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID)
SELECT REG_PROPERTY_ID
FROM REG_RESOURCE_PROPERTY
WHERE REG_PATH_ID IN
    (SELECT REG_PATH_ID
        FROM REG_RESOURCE);

-- Extract resource property (ID) created when versioning is enabled --

INSERT INTO TEMP_REG_RESOURCE_PROPERTY_ID(REG_PROPERTY_ID)
SELECT REG_PROPERTY_ID
FROM REG_RESOURCE_PROPERTY
WHERE REG_VERSION IN
    (SELECT REG_VERSION
        FROM REG_RESOURCE);

-- delete all unwanted REG_RESOURCE_PROPERTY entries --

DELETE
FROM REG_RESOURCE_PROPERTY
WHERE REG_PROPERTY_ID NOT IN
    (SELECT REG_PROPERTY_ID
    FROM TEMP_REG_RESOURCE_PROPERTY_ID);

-- delete all unwanted REG_PROPERTY entries --

DELETE
FROM REG_PROPERTY
WHERE REG_ID NOT IN
    (SELECT REG_PROPERTY_ID
    FROM TEMP_REG_RESOURCE_PROPERTY_ID);

-- drop temporary table --

DROP TABLE TEMP_REG_RESOURCE_PROPERTY_ID;


REG_TAG and REG_RESOURCE_TAG Table Cleanup

Code Block
languagesql
titleDB types: H2, MSSQL and Postgresql
CREATE TABLE TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID INTEGER);

-- Extract resource tag (ID) created when versioning is disabled --

INSERT INTO TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID)
SELECT REG_TAG_ID
FROM REG_RESOURCE_TAG
WHERE REG_PATH_ID IN
    (SELECT REG_PATH_ID
        FROM REG_RESOURCE);

-- Extract resource tag (ID) created when versioning is enabled --

INSERT INTO TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID)
SELECT REG_TAG_ID
FROM REG_RESOURCE_TAG
WHERE REG_VERSION IN
    (SELECT REG_VERSION
        FROM REG_RESOURCE);

-- Remove the foreign key constraint --

ALTER TABLE REG_RESOURCE_TAG DROP CONSTRAINT REG_RESOURCE_TAG_FK_BY_TAG_ID;

-- delete all unwanted REG_RESOURCE_TAG entries --

DELETE
FROM REG_RESOURCE_TAG
WHERE REG_TAG_ID NOT IN
    (SELECT REG_TAG_ID
    FROM TEMP_REG_RESOURCE_TAG_ID);

-- delete all unwanted REG_TAG entries --

DELETE
FROM REG_TAG
WHERE REG_ID NOT IN
    (SELECT REG_TAG_ID
    FROM TEMP_REG_RESOURCE_TAG_ID);

-- add the foreign key constraint back --

ALTER TABLE REG_RESOURCE_TAG ADD CONSTRAINT REG_RESOURCE_TAG_FK_BY_TAG_ID FOREIGN KEY (REG_TAG_ID, REG_TENANT_ID) REFERENCES REG_TAG (REG_ID, REG_TENANT_ID);

-- drop temporary table --

DROP TABLE TEMP_REG_RESOURCE_TAG_ID;
Code Block
languagesql
titleDB types: MySQL
CREATE TABLE TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID INTEGER);

-- Extract resource tag (ID) created when versioning is disabled --

INSERT INTO TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID)
SELECT REG_TAG_ID
FROM REG_RESOURCE_TAG
WHERE REG_PATH_ID IN
    (SELECT REG_PATH_ID
        FROM REG_RESOURCE);

-- Extract resource tag (ID) created when versioning is enabled --

INSERT INTO TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID)
SELECT REG_TAG_ID
FROM REG_RESOURCE_TAG
WHERE REG_VERSION IN
    (SELECT REG_VERSION
        FROM REG_RESOURCE);

-- Remove the foreign key constraint --

ALTER TABLE REG_RESOURCE_TAG DROP FOREIGN KEY REG_RESOURCE_TAG_FK_BY_TAG_ID;

-- delete all unwanted REG_RESOURCE_TAG entries --

DELETE
FROM REG_RESOURCE_TAG
WHERE REG_TAG_ID NOT IN
    (SELECT REG_TAG_ID
    FROM TEMP_REG_RESOURCE_TAG_ID);

-- delete all unwanted REG_TAG entries --

DELETE
FROM REG_TAG
WHERE REG_ID NOT IN
    (SELECT REG_TAG_ID
    FROM TEMP_REG_RESOURCE_TAG_ID);

-- add the foreign key constraint back --

ALTER TABLE REG_RESOURCE_TAG ADD CONSTRAINT REG_RESOURCE_TAG_FK_BY_TAG_ID FOREIGN KEY (REG_TAG_ID, REG_TENANT_ID) REFERENCES REG_TAG (REG_ID, REG_TENANT_ID);

-- drop temporary table --

DROP TABLE TEMP_REG_RESOURCE_TAG_ID;
Code Block
languagesql
titleDB types: DB2 and Oracle
CREATE TABLE TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID INTEGER);

-- Extract resource tag (ID) created when versioning is disabled --

INSERT INTO TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID)
SELECT REG_TAG_ID
FROM REG_RESOURCE_TAG
WHERE REG_PATH_ID IN
    (SELECT REG_PATH_ID
        FROM REG_RESOURCE);

-- Extract resource tag (ID) created when versioning is enabled --

INSERT INTO TEMP_REG_RESOURCE_TAG_ID(REG_TAG_ID)
SELECT REG_TAG_ID
FROM REG_RESOURCE_TAG
WHERE REG_VERSION IN
    (SELECT REG_VERSION
        FROM REG_RESOURCE);

-- delete all unwanted REG_RESOURCE_TAG entries --

DELETE
FROM REG_RESOURCE_TAG
WHERE REG_TAG_ID NOT IN
    (SELECT REG_TAG_ID
    FROM TEMP_REG_RESOURCE_TAG_ID);

-- delete all unwanted REG_TAG entries --

DELETE
FROM REG_TAG
WHERE REG_ID NOT IN
    (SELECT REG_TAG_ID
    FROM TEMP_REG_RESOURCE_TAG_ID);

-- drop temporary table --

DROP TABLE TEMP_REG_RESOURCE_TAG_ID;