There can be times when your client may be receiving huge amounts of data into the SalesForce on a daily basis, so much so that they may run out of storage space in their SalesForce. The excess amount of data received on a daily basis may also make the older data redundant but still important to trash it completely. Archiving the old data can be a suggested solution here.
A good approach at dealing with this issue is to archive the older leads in the CSV format and store them in the chatter feed post so it would count against SalesForce data storage rather than record storage SalesForce. Why chatter files? Well this makes it easier for administrators to view/share the files.
Here is how you can address this issue –
Start with setting a threshold of the leads that when exceeded should kickoff a batch job which archives oldest leads. Then, make the batch job Stateful (the state is preserved across all chunks of a batch) so that all the leads for the job can be put in one CSV file with a specific timestamp which makes it easier for future reference.
Under mentioned is the batchjob code that gets the oldest leads, writes them into a CSV file and inserts the file when all the chunks complete executing.