2

I have a table with many entries. I can very easily "delete" those entries, such that they no longer appear when using the Django Admin page. However, these records are still in the .sqlite3 file. I would like to reduce the .sqlite3 file size as a result of my deletion.

Google searches are so far fruitless on the topic.

** edit **

This should not be marked as a duplicate. While the question has been asked in other forms before, none of them are specifically tailored to using sqlite3 through a Django instance.

AndrewGrant
  • 786
  • 7
  • 17
  • How do you delete those entries? If you end up deleting them through the `.delete()` method, they should not be on the DB file anymore. – Cyrlop Jul 21 '18 at 23:34
  • They are still there . I've used .delete() from the Shell, and deleted via the admin interface – AndrewGrant Jul 21 '18 at 23:39
  • After deleting the instances running `sqlite3 /path/to/foo.db 'VACUUM;' ` will reduce the size – Anupam Mar 12 '20 at 06:51

1 Answers1

1

The following method works for me...

python3 manage.py shell
>>> from OpenBench.models import Result
>>> r = Result.objects.filter()
>>> r._raw_delete(r.db)
>>>
>>> from django.db import connection
>>> cursor = connection.cursor()
>>> cursor.execute("vacuum;")
>>> quit()

This deletes all Result entries, and also reduces the size of the database file.

AndrewGrant
  • 786
  • 7
  • 17