I don’t see why the number of pages is a concern here. The snippet removes old versions for all the pages. Just takes more time to execute if there are a lot of pages/versions.
Also, you may want to look at this thread with the same topic which is quite recent.