Improve delete handling code.

Change-Id: I3f09f00a2966ee246056ea11ec55d952da6aabc0