In a case where a docker image is being built, it generates layers which are unnamed and untagged. This causes the cleanup script to fail when trying to clean them.
An example of such a failure can be found here
The quick fix would be to ignore those trailing layers or alternatively detect them and remove them by image id. Imo, the second option is safer because every 'trailing layer' like that may get large (1GB+).
The long-term solution would be to implement some sort of garbage collector for orphaned layers.
I've found an open ticket  with some optional solutions, still need to see if they fit for our use case. If not, maybe consider implementing something similar.