Thank you so much!! It now rebuilds the hash without gaps and I can delete the first, last or middle images and have it work perfectly. Though I don't see why removing the second foreach did the trick (I only deleted one time), but it works like a charm.
The second foreach would always delete the last thing in the hash (where last thing means after the first delete). This works where you've moved everything up to close up a gap, since the last entry would be an invalid one then. It doesn't work where you've deleted the last entry; it goes ahead and deletes what was originally the second to last entry.
Try writing down on paper what happens at each step, with keys 1, 2, and 3 and deleting key 3.