Skip to content

Instantly share code, notes, and snippets.

@romilly
Created July 17, 2022 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romilly/5a1ff86d1e4d87e084b76d5651f23a40 to your computer and use it in GitHub Desktop.
Save romilly/5a1ff86d1e4d87e084b76d5651f23a40 to your computer and use it in GitHub Desktop.
Delete all files and directories from a micropython file-system (including .py files)
# ***WARNING***
# Running this file will delete all files and directories from the micropython device it's running on
# If you run keep_this=False it will delete this file as well.
# see https://docs.micropython.org/en/latest/library/os.html for os function list
import os
def _delete_all(directory='.', keep_this=True):
try:
import machine
except:
# not a micropython board so exit gracefully
print('Not a micro-python board! Leaving it well alone.')
return
for fi in os.ilistdir(directory):
fn, ft = fi[0:2] # can be 3 or 4 items returned!
if keep_this and fn == '_nuke.py':
continue
fp = '%s/%s' % (directory, fn)
print('removing %s' % fp)
if ft == 0x8000:
os.remove(fp)
else:
_delete_all(fp)
os.rmdir(fp)
_delete_all()
@romilly
Copy link
Author

romilly commented Jul 17, 2022

I needed this to help me create an automated deployment process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment