Is there any way to handle the equivalent of Python's GPIO.cleanup() via the terminal ?

  • Make a small python script and call it from terminal. That's the easiest way I suppose. Commented Nov 5, 2015 at 5:55

2 Answers 2

A Small Bash script/python script will do the job. Run it on raspberry Pi.


RPi.GPIO provides a built-in function GPIO.cleanup() to clean up all the ports you’ve used. But be very clear what this does. It only affects any ports you have set in the current program. It resets any ports you have used in this program back to input mode. This prevents damage from, say, a situation where you have a port set HIGH as an output and you accidentally connect it to GND (LOW), which would short-circuit the port and possibly fry it. Inputs can handle either 0V (LOW) or 3.3V (HIGH), so it’s safer to leave ports as inputs.


import RPi.GPIO as GPIO  

# the rest of your code would go here 

# when your code ends, the last line before the program exits would be...  

# remember, a program doesn't necessarily exit at the last line! 

For more in-depth check this link.

  • "It resets any ports you have used in this program back to input mode" - in other words it DOES NOTHING.
    – Milliways
    Commented Nov 5, 2015 at 22:38

The following code will set all the exported GPIO to be unexported inputs.

sudo bash -c "for ((i=0; i<32; i++)); do echo \$i; echo in >/sys/class/gpio/gpio\$i/direction; echo \$i >/sys/class/gpio/unexport; done"

Note that the \ before the $i are important to prevent premature shell expansion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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