-->
With an Example Fix: How To Delete Existing Migration Files Flask Python / Directory migrations already exists and is not empty

With an Example Fix: How To Delete Existing Migration Files Flask Python / Directory migrations already exists and is not empty

Back to top

Updated by Ashirafu Kibalama on June 01, 2024

With an Example Learn To Fix: Delete Existing Migration Files Flask Python/ Directory migrations already exists and is not empty




Migrations are essential for managing database schema changes in Flask applications. 


However, there are times when you need to delete existing migration files, whether to start fresh or for other reasons. 


In this blog post, we'll explore the process of deleting existing migration files in a Flask-Python application.


We'll cover the necessary steps to safely remove migration files, including navigating to the migrations directory, deleting the files, and optionally reinitializing Flask-Migrate. 


Additionally, we'll provide platform-specific commands for Unix/Linux/macOS, Windows Command Prompt, and PowerShell.


Following the instructions in this guide, you'll learn how to effectively manage migration files in your Flask application, ensuring a clean and organized database migration workflow. 


4 Steps To Delete Existing Migration Files Flask Python


To delete existing migration files in a Flask application that uses Flask-Migrate for managing database migrations, you can follow these steps:


1) Navigate to the Migrations Directory

Go to the directory where your Flask app is located using the terminal or command prompt.








2) Delete Migration Files

You can delete the existing migration files in the correct directory. 


Typically, these files are located in a directory named migrations. 

You can use the appropriate command based on your operating system:



On Unix/Linux/macOS:



rm -rf migrations/*


On Windows (using Command Prompt):



rmdir /s /q migrations


#after deleting using command prompt:





On Windows (using PowerShell):



Remove-Item -Recurse -Force .\migrations


#after deleting using powershell:







#before deleting







Ensure you execute these commands in your Flask application's root directory.


3) Reinitialize Migrations (Optional)

After deleting the migration files, you should reinitialize Flask-Migrate to start fresh. 


You can do this by running the following commands:



flask db init


#Here am not using command  Prompt:






Click Here To; Fix Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the curren

t directory.







Fix Error: Directory migrations already exists and is not empty


Here's a workaround you can try to Fix Error: Directory migrations already exists and is not empty:


1- Rename the migrations directory

First, rename the existing migrations directory to something else. For example, you can rename it to migrations_backup.



mv migrations migrations_backup

# press enter




#wait for a minute for the system to process:





2- Initialize migrations

Now, initialize migrations afresh. Run the following command to create a new migrations directory:



flask db init




This command will create a new migrations directory with the necessary files.


3- Generate initial migration

Generate the initial migration based on your current database schema:



flask db migrate -m "Initial migration"



#"If you come across this:"




Click here to Fix ERROR [flask_migrate] Error: Can't locate revision identified by '7eb279738ff9':



4- Apply the migration

Apply the migration to your database:



flask db upgrade



4) Verify

After deleting the migration files, you should verify that the migrations directory is empty or only contains the newly generated migration script (if you reinitialized Flask-Migrate). 


You can do this by listing the contents of the migrations directory:



ls migrations # On Unix/Linux/macOS
dir migrations # On Windows



Following these steps, you can delete existing migration files in your Flask application. 


However, be cautious when deleting migration files, as this can lead to data loss if not handled properly. 


Make sure to back up your database or have a plan to recreate it if necessary.


Please let us know if this post was helpful or if we missed anything. Your feedback is valuable to us.


"Thank you, and have a great time coding!"


Related Posts:

1) With an Example fix: Flask Migrate is not Working in Sqlalchemy


2) With an Example Fix: Sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) No Such Column:


3) Which is Better, SQLite or SQLAlchemy?


4) What Are The Purposes of Flask-SQLAlchemy in Python? 


5) When Should You Use SQLAlchemy? 


6) What are The Major Benefits of Using SQLAlchemy in Python?


7) What are The Differences Between Flask-SQLAlchemy and SQLAlchemy? 


8) What are The Disadvantages of Flask-SQLAlchemy?


9) Sqlalchemy-migrate vs Alembic 


10) Flask-migrate vs Alembic