Help! Not all my objects are being cleaned
Published 11 January 2023
The clean command drops all objects in the configured schemas. This is sometimes needed in Flyway Desktop to reset the Shadow Database (learn more). Some of our deployment pipeline examples also use flyway clean during the build, to clean and then migrate the database in our CI environment to test building the database from scratch.
There are some objects that will not be cleaned:
- SQL Server - no users will be dropped
Cleaning additional objects
For complicated database structures, an accurate dependency graph cannot always be constructed, so not every object is cleaned. To clean additional objects, you can add an afterClean callback with SQL statements that will drop any of the objects left behind in your database after running flyway clean. The afterClean.sql callback needs to be added to the locations folder that contains your migrations. This should be committed to your repository for a consistent clean across dev/test environments.
For example:
afterClean.sql
-- Our test_user is created when we run the baseline script -- We need to make sure it is removed after we clean the database -- so there are no errors when we re-apply the baseline DROP USER test_user