Last active
January 14, 2025 17:27
-
-
Save gokhanozdemir/82756391ad81538bfdc209ba3989fbba to your computer and use it in GitHub Desktop.
By implementing the provided SQL script, users can proactively address these issues, ensuring their Directus instance remains efficient, organized, and aligned with their evolving content management needs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Directus Database Cleanup Script | |
-- Purpose: Remove orphaned data and optimize database performance in Directus CMS | |
-- Tested on: Directus 11.2.2 Postgres 16.0 | |
-- WARNING: Always backup your database before running cleanup scripts! | |
-- DISCLAIMER: Use at your own risk! | |
/** | |
* Background: | |
* During CMS development, especially with Directus, database structure changes frequently. | |
* This can lead to: | |
* - Accumulated orphaned data from deleted collections | |
* - Unnecessary database bloat from logs and revisions | |
* - Decreased query performance | |
* - Storage space issues, especially on free-tier platforms | |
* | |
* Prerequisites: | |
* 1. Create a complete backup of your database | |
* 2. Test on a non-production environment first | |
* 3. Consider disabling activity tracking on collections before running | |
* 4. Ensure Directus is not running during cleanup | |
* 5. Delete any unused collections before running (if they still exist in the database but not visible on Directus) | |
* Note: Re-enabling tracking may cause errors (not tested) | |
* | |
* Usage Notes: | |
* - Script removes activity logs and revisions for non-existent collections | |
* - Performs database optimization after cleanup | |
* - Particularly useful when approaching storage limits | |
* Motivation: | |
I had ~1 GB of SQL data, primarily consisting of logs. After running this script, and the one below my database size reduced to ~300 MB.Yours may vary. | |
* This effort is inspired from: https://gist.github.com/madc/cee04370227a12127361a615d2ae8678 | |
* Thank you for sharing your sql and inspiring me research more on this topic.@macd | |
*/ | |
-- Step 1: Remove orphaned activity logs | |
-- Deletes entries from directus_activity where referenced collections no longer exist | |
DELETE FROM public.directus_activity | |
WHERE collection NOT IN ( | |
SELECT tablename | |
FROM pg_tables | |
WHERE schemaname = 'public' | |
); | |
-- Step 2: Clean revision history | |
-- Removes revision records for deleted collections | |
DELETE FROM public.directus_revisions | |
WHERE collection NOT IN ( | |
SELECT tablename | |
FROM pg_tables | |
WHERE schemaname = 'public' | |
); | |
-- Step 3: Optimize database | |
-- Reclaims storage space and updates query planner statistics | |
VACUUM ANALYZE; | |
/** | |
* Post-Execution: | |
* 1. Verify system functionality | |
* 2. Monitor system performance | |
* 3. Consider implementing regular cleanup schedule via flows | |
*/ |
Author
gokhanozdemir
commented
Jan 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment