Core Data externally stored binary data not deleted with record

I have an image field on a Core Data entity with "Allows External Storage" enabled. When I delete a record, the external binary data file remains on disk. How can I ensure that all externally stored data is deleted along with the record?

Replies

Hi ! You will have to override -didSave in your image managed object subclass and do the deletion of the file. See the following discussions: https://stackoverflow.com/questions/17769595/the-best-practice-for-deleting-core-data-items-where-path-is-stored-as-text-and

https://stackoverflow.com/questions/5073113/how-to-handle-cleanup-of-external-data-when-deleting-core-data-objects

Those look like questions about cases where the file URL is stored in the field. I'm storing binary data in the field, and Core Data is automatically storing blobs beyond a certain threshold as external files. Those files remain on disk after the records are deleted. Since the file creation is automatic in Core Data, I would expect the files to be deleted when the record is deleted, but they're not. I also can't find a way to access the file URL of the record's external data to delete the files manually.