9

I am programatically trying to check the size of SQLite database stored on the iphone after deleting some rows. The records get deleted, but the file size remains the same. Is there any way I can get the correct size of file after deleting records from sqlite in iOS? Please see the code below:

NSString* databasePath = [[NSString alloc]
                              initWithString: [self.documentsDirectory stringByAppendingPathComponent: self.databaseFilename]];
unsigned long long fileSize = [[[NSFileManager defaultManager]
                                    attributesOfItemAtPath:databasePath error:nil] fileSize];

Thanks!

I cant use the vaccum approach because it deletes all the records . I am deleting 50% of data only, and not all. So this is not duplicate of this question change sqlite file size after "DELETE FROM table"

Community
  • 1
  • 1
Nilesh Agrawal
  • 3,002
  • 10
  • 26
  • 54
  • 2
    Possible duplicate of [change sqlite file size after "DELETE FROM table"](http://stackoverflow.com/questions/2143800/change-sqlite-file-size-after-delete-from-table) – Thilo Mar 04 '16 at 03:13
  • It is normal for the file size to not decrease. The database will reclaim the space as needed. If you want to force it to rewrite a smaller file, run vacuum. – Thilo Mar 04 '16 at 03:14
  • Thilo, I am deleting only some records and not all the records. Vaccum helps in case of deleting all the records. – Nilesh Agrawal Mar 04 '16 at 03:15
  • 4
    Vacuum does not delete any data. It just compacts the space used to store it (potentially resulting in a smaller file). – Thilo Mar 04 '16 at 03:16

1 Answers1

10

The size of a SQLite database file does not necessarily shrink when records are deleted. If auto_vacuum is not enabled, it will never shrink unless you perform a vacuum operation on it.

See https://sqlite.org/lang_vacuum.html and https://sqlite.org/pragma.html#pragma_auto_vacuum .

Steve Jorgensen
  • 11,725
  • 1
  • 33
  • 43