2

I know I can use fastboot -w or fastboot erase userdata, but these erase /data/media/, the internal storage which is located at /data/media/0/..., which I don't want.

Is there a way that doesn't include hitting the factory reset option in your recovery, but doing it manually? I'm using ClockWorkMod.

  • worked like a charm, recoveries kept screwing me over but this seemed to do the trick. hasn't fixed my issue completely, but it might just need longer to wipe. how long does it usually take? shouldn't be too long at all right – applepieftw Nov 14 '15 at 17:09
  • 2
    @Firelord Please make that an answer (but include it requires root powers) :) for dir in /data/*; do if [[ $dir =~ media ]]; then continue; else rm -rf $dir; done – I really love shell power :) Oh: as the regex compare might fail: if [ "$dir" = "/data/media" ] should work as well. You can test it safely replacing the rm -rf with the echo command. – Izzy Nov 14 '15 at 19:07
2

Boot into your custom Recovery, mount the data partition either using command-line or via GUI, and choose any of the following method:

(Note that TWRP already has the option to clear /data without touching /data/media. It uses rm -rf commands1.)

Go Immune

  1. Make /data/media immutable2 i.e. immune from any modification. You can do so by executing:

    adb shell chattr +i /data/media
    

    You can also use -R for recursive immunity, though it was not needed in my test case.

    You can see the immune attribute (i) in the output of

    adb shell lsattr /data/
    
  2. Time to go berserk

    adb shell rm -rf /data/*
    

    All the files and their holding directories inside /data/ should now be purged except the ones inside media, for which you would get the error (non-critical) Permission denied for every file inside that directory.

  3. Now revoke the immunity using

    adb shell chattr -i /data/media
    

    Use -R if you used it previously.

If-Else

Manually pick each directory under /data and use rm -rf on it. Could be tiresome, so you may consider Izzy's shell-fu

adb shell for dir in /data/*; do if [[ "$dir" = "/data/media" ]]; then continue; else rm -rf "$dir"; fi; done;

Replace rm -rf with echo for a dry run.

The command picks up each file-path3 under /data, then checks against whether the file-path is for media folder: if yes, then pass; else nuke the file.


1: TWRP FAQ: What is a data/media device?
2: Wikipedia: chattr
3: General overview of the Linux file system: Sorts of files

| improve this answer | |
  • Would making a partition immune be persistent after a reboot? – SarpSTA Nov 16 '15 at 4:33
  • The last time I checked, immunity is set as an attribute to files and not to a filesystem. If you want your filesystem to remain unmodified then mount it in read-only mode. – Firelord Nov 16 '15 at 4:35
  • Even if it was immune though, it probably wouldn't survive a .kdz flash would it? – SarpSTA Nov 16 '15 at 4:36
  • 1
    The attributes are enforced in an environment where the latter understands those attributes. Besides, the flashing is usually done on partition level so once the partition is unmounted the attribute is of no value. – Firelord Nov 16 '15 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.