Cleaning up old data in Ansible Tower

Older Ansible Tower versions don’t have cleanup tasks scheduled by default, which may lead to a very slow or even unusable Tower instance with lots of historic data. At that point it may not even be possible to schedule cleanup task and even if they are set up with lots of patience they may be so slow that they timeout. Luckily there’s a command line tool called awx-manage, but in my case even that failed as the cleanup tasks also timed out.

At that point the only remedy is to manually delete data in Tower. I managed to unstuck a Ansible Tower 2.1.4 instance as follows:

Run the Tower shell via awx-manage (ideally in a screen session, this may take a while):

sudo awx-manage shell_plus

Paste the following code into the shell to cleanup objects in the database:

from awx.main.models import Job
for record_id in range(1, 200000):
    try:
        db_object = Job.objects.get(id=record_id)
        db_object.delete()
    except:
        pass

This iterates through all Job objects up to record_id=200000 and deletes them. You may have a lot less or a lot more objects in your Tower database, so you might have to adjust the 200000 to something else. I also had to delete the UnifiedJob objects, so you also might have to run this:

from awx.main.models import UnifiedJob
for record_id in range(1, 200000):
    try:
        db_object = UnifiedJob.objects.get(id=record_id)
        db_object.delete()
    except:
        pass

This took several hours in my case, your mileage may vary. After this manual deletion went through the following awx-manage cleanup commands started working again for me:

sudo awx-manage cleanup_deleted --days=0
sudo awx-manage cleanup_jobs --days=0
sudo awx-manage cleanup_activitystream --days=0

Finally I restarted Ansible Tower and the webinterface was reasonably fast again. Hope it works for you, too!


Any comments? Ping me on Twitter. 👉🏻 Get my newsletter for occasional updates. ✌🏻