Skip to content

Fix Flyway Clean Repair โ€“ Clean Not Removing Objects

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Fix Flyway Clean Repair. We cover key concepts, practical examples, and best practices.

You run flyway clean to reset a development database, but some tables, sequences, or functions remain. Or flyway repair completes without errors, but the flyway_schema_history still shows the wrong migration state.

Wrong โŒ

flyway clean

Output:

Successfully cleaned schema "public" (execution time 00:00.234s)

But when you check:

```sql
\dt
-- Some tables still exist!

Or:

```bash
flyway repair

Output:

Successfully repaired schema history table "public"."flyway_schema_history"

But:

```bash
flyway info
# Still shows a failed migration with "Pending" status

## Right โœ…

**Ensure clean includes all object types:**

```bash
# flyway.conf
flyway.cleanDisabled=false            # default: false
flyway.cleanMode=ALL                  # drops all objects (default)

If `flyway.cleanMode=SCHEMA`:

```bash
flyway.cleanMode=SCHEMA               # drops schema and recreates it
# vs
flyway.cleanMode=ALL                  # drops all objects individually

Use `SCHEMA` mode for <a href="/databases/postgresql/">PostgreSQL</a> โ€” it drops and recreates the schema, which removes everything including custom types and extensions.

**Run clean with multiple schemas:**

```bash
flyway.cleanMode=ALL
flyway.schemas=public,app,audit
flyway clean
# Cleans all three schemas

**Check for extensions and FDW:**

Some objects (foreign data wrappers, extensions) are outside the schema and not cleaned:

```sql
-- Manually drop extensions if needed
DROP EXTENSION IF EXISTS "uuid-ossp" CASCADE;
DROP EXTENSION IF EXISTS "postgis" CASCADE;

**Debug what clean will do:**

```bash
flyway clean -outputType=json
# Shows all objects that will be dropped

**For repair โ€” check the schema history table:**

```sql
SELECT version, description, installed_on, success
FROM flyway_schema_history
ORDER BY installed_rank;

If a migration shows `success=false`, Flyway won't retry it automatically. You need to:

```bash
# 1. Manually fix the schema
# 2. Delete the failed row:
DELETE FROM flyway_schema_history WHERE success = false;
# 3. Run flyway migrate again

Or reset entirely:

```bash
flyway clean
flyway migrate

## Root Cause

`flyway clean` only drops objects within the schemas managed by Flyway. Extensions, FDWs, and objects in other schemas are not removed. `flyway repair` fixes checksums and removes failed migration entries โ€” but only if the `flyway_schema_history` table is in a consistent state.

## Prevention

- Use `flyway.cleanMode=SCHEMA` for <a href="/databases/postgresql/">PostgreSQL</a> โ€” it drops and recreates schemas.
- Keep extensions in a separate migration script so you know what to reโ€‘create.
- Use `flyway clean` only in development/CI, never in production.
- Run `flyway info` before and after clean/repair to verify the state.


## Common Mistakes with clean repair

1. **Using `foldl` instead of `foldl'` causing stack overflow on large lists**
2. **Forgetting `deriving (Show, Eq)` on custom data types needed for debugging**
3. **Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable**

These mistakes appear frequently in real-world FLYWAY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

## Practice Exercise

**Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.**

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I clean specific tables only?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: No โ€” <code>flyway clean</code> drops <strong>all</strong> objects in the managed schemas.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Will clean break other applications using the same database?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes โ€” clean drops everything. Only use it on dedicated development databases.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Why does repair say "nothing to repair" when I have a failed migration?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Repair only fixes checksum mismatches and removes empty migration rows. It won't retry failed migrations.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Does clean work on read replicas?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: No โ€” clean only works on the primary database where Flyway has write access.</p>
</div></details>

---

*Database reset strategies are covered in the [DodaTech Flyway Operations course](https://dodatech.com/courses/flyway).*

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro