Skip to content

Instantly share code, notes, and snippets.

@Afeez1131
Created August 13, 2024 08:02
Show Gist options
  • Save Afeez1131/1b2fa01c8ce461b56f7f5a5cdd3e09eb to your computer and use it in GitHub Desktop.
Save Afeez1131/1b2fa01c8ce461b56f7f5a5cdd3e09eb to your computer and use it in GitHub Desktop.
Python script to delete django migrations from all app specified.
import os
import glob
def delete_migrations(app_name):
# Get the path to the migrations folder of the app
migrations_path = os.path.join(app_name, 'migrations')
# Check if the migrations folder exists
if os.path.exists(migrations_path):
# Find all migration files (except __init__.py) in the migrations folder
migration_files = glob.glob(os.path.join(migrations_path, '*.py'))
migration_files += glob.glob(os.path.join(migrations_path, '*.pyc'))
# Loop through and delete each file (except __init__.py)
for migration_file in migration_files:
if os.path.basename(migration_file) != '__init__.py':
os.remove(migration_file)
print(f"Deleted: {migration_file}")
print("All migrations deleted successfully.")
else:
print(f"The app '{app_name}' does not have a migrations directory.")
if __name__ == "__main__":
app_name = input("Enter the Django app names (seperated by ',' for multiple at once): ")
app_name_list = app_name.split(',')
for app in app_name_list:
delete_migrations(app.lstrip().rstrip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment