Sorry, I thought you meant keyframes. In order to delete (or add) actual frames, Python has to play a trick, which only works when there's a figure in the scene. The attached file holds an example of Fake Frames. The little code at the bottom is just to test the main routine; you would include the routine in your own code and call it in the same way.
My python page
My ShareCG freebies