How to erase unsued space so that dumping userdata partition creates a sparse file of small size? When I create a gzip image of userdata partition (/dev/block/mmcblk0p29) it creates huge 6.8GB image, previously it was 350MB.

Is it possible through command-line to erase or wipe or delete space on userdata partition? I want to keep small compact compressed gzip or shadow images of clean unrooted and rooted partition on various dates and times.

I tried the following commands but nothing worked for me:


fastboot erase userdata
rm -r /data/*.*
rm -r /sdcard/*.*


Still image is 6.8GB.


dd if=/dev/zero if=/dev/block/platform/7824900.sdhci/by-name/userdata bs=8192
dd if=/dev/urandom of=/dev/block/platform/7824900.sdhci/by-name/userdata bs=8192


/dev/zero and /dev/urandom not found in device.


newfs
iShredder 3 app
Secure Eraser app
Andro Shredder or Disk Sanitizer


But how will I format in unrooted while working in the Android userdata partition?


Any adb shell command to clear, format or zero fill
i guess your real purpose of this question is not what the headline suggests - secure erase your data - but instead you want to have a full backup of userdata including /data/media (downvote if i am wrong)  

you have some options, most require additional binaries which you can copy to /cache partition or permanently integrate into your twrp /sbin  


e2image (thx @IrfanLatif)
make twrp backup of /data to internal storage and just adb pull -a /sdcard afterwards, which you can zip without worry about file permissions  
just create tar.gz instead of img.gz with gnu tar (with --selinux --xattrs -p flags)  
create empty disk image and copy all (mke2fs + busybox cp -ac) then compress img with gz (requires much disk space and underlying non-fat32 file system type)  
permanent tweak for twrp  


(i will improve answer when i have time)