Unrar – Delete archive(s) after extraction

Due to lack of disk space on a server I needed a mechanism for automatically deleting RAR archives after their contents had been extracted. I couldn’t find any particularly neat way to do this with a shell script so I knocked up the following quick & dirty patch for unrar.

The patch applies cleanly to version 5.1.6 of the unrar source as follows:

wget 'http://www.rarlab.com/rar/unrarsrc-5.1.6.tar.gz'
tar -xvzf unrarsrc-5.1.6.tar.gz
cd unrar
wget 'http://simon.aldrich.eu/download/unrar/unrar-deleteOnExtract.patch'
patch -p1 < unrar-deleteOnExtract.patch
make

The compiled unrar binary will have the following extra option:

de  Delete archive(s) after extracting

Which you can use with the -e or -x commands e.g.:

unrar x -de test.part01.rar

Hopefully this might be useful to someone other than just me. Although, obviously, if extraction fails for any reason you will have deleted all the earlier volumes in the archive set. My suggestion would be to use the -t command to test your archive(s) first. I take no responsibility for any screw-ups you may cause yourself if you use this patch – caveat emptor!

7 thoughts on “Unrar – Delete archive(s) after extraction

  1. Jem North

    Why wouldn’t
    unrar x archive.rar && rm -rf archive.rar

    Work then? Not that I know anything of unrar, but I’d hope that it succeeds if the extraction works and fails otherwise?

    Wakka-chikka…..

    Reply
    1. Simon Aldrich Post author

      That works for the simple case when you only have a single archive to extract. However (un)rar supports multiple-volume archives where a set of archive files contain the compressed data. That’s mostly what this patch is to cover. 🙂

      Reply
  2. Mike

    Well done Simon! Indeed the most reliable way to delete rars after unpacking.
    Is your patch in the mainstream unrar yet? Is there a safer version with deleting rar files after all have been successfully unpacked?

    Reply
  3. Mike

    Hi Simon!

    Just wanted to say thank you!
    You saved me a lot of bash-scripting here!

    Cheers from sweden!

    Reply
  4. Brian

    It appears the -de option is no longer available, is there an undocumented option that works? Using Ubuntu 21.10

    Regards,
    Brian

    Reply
  5. Pekka Rastas

    Thanks man or person!

    I was looking for a way to do this with Python or bash, but this is so much simpler.

    ‘find -type f -iname “*.rar” -execdir unrar x -de {} \;’

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *