In Java, System.clearProperty(key)removes the system property named key and returns the value it had, or null if it was not set. Afterwards getProperty(key) returns null (or its default) again.
It is the opposite of setProperty, and the only clean way to make a property "missing" again: setProperty(key, null) throws, and setProperty(key, "") leaves an empty value behind, which is not the same as no value.
A static method of the java.lang.System class, added in Java 5 (JDK 1.5). Call it on the class name, as in System.clearProperty(...). No import is needed, because classes in java.lang are always visible.
Throws away all changes and recreates the startup set.
getProperties().remove(key)
Same effect, but no key check and an Object result.
Java Examples
Example 1 — Remove a Property and See What Comes Back
Set a property, clear it, then clear it again.
Java
public class SystemClearPropertyBasic {
public static void main(String[] args) {
System.setProperty("app.cache", "on");
String removed = System.clearProperty("app.cache");
String again = System.clearProperty("app.cache");
System.out.println("Removed value: " + removed);
System.out.println("Value now: " + System.getProperty("app.cache"));
System.out.println("Second clear returned: " + again);
}
}
Output
Removed value: on
Value now: null
Second clear returned: null
How It Works
The removed value comes back. "app.cache" held "on", so clearProperty deletes it and returns "on".
Gone, not empty. After clearing, getProperty("app.cache") returns null, exactly as if the key had never been set.
Clearing twice is harmless. The second call finds nothing to remove and returns null instead of throwing.
Example 2 — Turn Feature Flags Off Again
Boolean.getBoolean and Integer.getInteger read system properties. Clearing the keys brings back their default behaviour.
Java
public class SystemClearPropertyFlag {
public static void main(String[] args) {
System.setProperty("feature.beta", "true");
System.out.println("Beta on: " + Boolean.getBoolean("feature.beta"));
System.out.println("Retries: " + Integer.getInteger("feature.retries", 3));
System.setProperty("feature.retries", "5");
System.out.println("Retries: " + Integer.getInteger("feature.retries", 3));
System.clearProperty("feature.beta");
System.clearProperty("feature.retries");
System.out.println("Beta on: " + Boolean.getBoolean("feature.beta"));
System.out.println("Retries: " + Integer.getInteger("feature.retries", 3));
}
}
Output
Beta on: true
Retries: 3
Retries: 5
Beta on: false
Retries: 3
How It Works
Flags read from properties.Boolean.getBoolean("feature.beta") is true because the property is "true"; Integer.getInteger uses the fallback 3 while "feature.retries" is missing.
Parsed for you. After setting "feature.retries" to "5", Integer.getInteger parses the text and returns 5.
Back to defaults. With both keys cleared, the flag reads false and the retry count falls back to 3, the same as before anything was set.
Example 3 — Clean Up Every Key a Test Created
Remember the keys you set, then clear them all at the end and count how many were really removed.
Java
public class SystemClearPropertyCleanup {
public static void main(String[] args) {
String[] keys = {"test.user", "test.region", "test.limit"};
String[] values = {"demo", "eu", "10"};
for (int i = 0; i < keys.length; i++) {
System.setProperty(keys[i], values[i]);
}
int removed = 0;
for (String key : keys) {
if (System.clearProperty(key) != null) {
removed++;
}
}
System.out.println("Removed: " + removed);
System.out.println("test.region now: " + System.getProperty("test.region"));
try {
System.clearProperty("");
} catch (IllegalArgumentException e) {
System.out.println("Empty key: " + e.getClass().getSimpleName());
}
}
}
Parallel arrays. The first loop sets three keys from keys to the matching entries of values.
Count real removals. Each clearProperty returns the old value; all three were set, so none returns null and the count is 3.
Keys must not be empty.clearProperty("") is rejected with IllegalArgumentException, just like getProperty("") and setProperty("", v).
Frequently Asked Questions
The value the property had before it was removed, or null if the property was not set.
clearProperty removes the key, so getProperty returns null. setProperty with an empty string keeps the key with an empty value, so getProperty returns "".
Java 5 (JDK 1.5). Before that, code used System.getProperties().remove(key).
No. It just returns null. It only throws for a null key (NullPointerException) or an empty key (IllegalArgumentException).
No. Other code, including the JDK, may expect them. Only clear properties your own program created.